0

How would I change this to go into the private void? It is currently in the public static void. The main problem is that in the private void it cant read string. I am quite new to programming so please be patient if this is quite easy to do.

import java.util.Arrays;
import java.util.HashMap;

public class FrequencyPrint {
    public static void main(String[] args) {
        String s = "ccrrcdcffcghijk";
        HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();
        for (int i = 0; i < s.length(); i++) {
            if (hashMap.containsKey(s.charAt(i))) {
                int value = hashMap.get(s.charAt(i));
                hashMap.put(s.charAt(i), ++value);
            } else {
                hashMap.put(s.charAt(i), 1);
            }
        }

        Character keys[] = Arrays.copyOf(hashMap.keySet().toArray(), hashMap
                .keySet().toArray().length, Character[].class);
        Integer values[] = Arrays.copyOf(hashMap.values().toArray(), hashMap
                .values().toArray().length, Integer[].class);


        for (int i = 0; i < keys.length; i++) {
            int x = FrequencyPrint.findmax(values);
            for (int j = 0; j < values[x]; j++) {
                System.out.print(keys[x]);
            }
            values[x] = 0;
        }

    }

    public static int findmax(Integer values[]) {
        int max = 0;
        for (int i = 0; i < values.length; i++) {
            if (values[i] > values[max]) {
                max = i;
            }
        }
        return max;
    }
}
  • what do you want to set private? findmax? the main method? the public method has always to be public. – Klemens Morbe Mar 18 '14 at 16:29
  • If `main()` you "can't"; it must be `public static` for it to be usable. You can put other modifiers but then it won't run – fge Mar 18 '14 at 16:31

2 Answers2

0
import java.util.Arrays;
import java.util.HashMap;

public class FrequencyPrint {
    public static void main(String[] args) {
        FrequencyPrint fP = new FrequencyPrint();
        fP.readStrings();
    }

    private void readStrings() {
        String s = "ccrrcdcffcghijk";
        HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();
        for (int i = 0; i < s.length(); i++) {
            if (hashMap.containsKey(s.charAt(i))) {
                int value = hashMap.get(s.charAt(i));
                hashMap.put(s.charAt(i), ++value);
            } else {
                hashMap.put(s.charAt(i), 1);
            }
        }

        Character keys[] = Arrays.copyOf(hashMap.keySet().toArray(), hashMap
            .keySet().toArray().length, Character[].class);
        Integer values[] = Arrays.copyOf(hashMap.values().toArray(), hashMap
            .values().toArray().length, Integer[].class);


        for (int i = 0; i < keys.length; i++) {
            int x = this.findmax(values);
            for (int j = 0; j < values[x]; j++) {
                System.out.print(keys[x]);
            }
            values[x] = 0;
        }

    }

    private int findmax(Integer values[]) {
        int max = 0;
        for (int i = 0; i < values.length; i++) {
            if (values[i] > values[max]) {
                max = i;
            }
        }
        return max;
    }
}
Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
0

You want to return an int, but you are passing an Integer[] as argument. You basically have 4 options

  1. keep the method as it is (suggested..). Now, if you are bent upon keeping the return type as private void, then

  2. Create an object of FrequenceyPrint in main(), call findMax() using the object's reference, pass an Integer/object which encapsulates an int/Integer as argument . change the object referenced by the reference in findMax, your main() will get back the data

TheLostMind
  • 35,966
  • 12
  • 68
  • 104