-1

So I am trying to complete this code. The goal is to input an array of strings, then count the frequency of how often the words are found. For example:

input:

joe 
jim 
jack
jim
joe

output:

joe 2
jim 2
jack 1
jim 2
joe 2

An array must be chosen for Strings, and another array much be chosen for word frequency.

My code so far:

I am stuck into trying to implement this. The string method is set, but how am I going to count the frequency of words, and also assign those values to an array. Then print both side by side. I do know that once the integer array is set. We can simply do a for loop to print the values together such as. System.out.println(String[i] + " " + countarray[i]);

public class LabClass {

    public static int getFrequencyOfWord(String[] wordsList, int listSize, String currWord) {
        int freq = 0;

        for (int i = 0; i < listSize; i++) {
            if (wordsList[i].compareTo(currWord) == 0) {
                freq++;
            }
        }

        return freq;
    }

    public static void main(String[] args) {
        LabClass scall = new LabClass();
        Scanner scnr = new Scanner(System.in);

        // assignments 
        int listSize = 0;
        System.out.println("Enter list Amount");

        listSize = scnr.nextInt();
        // removing line to allow input of integer
        int size = listSize;   // array length

        // end of assignments
        String[] wordsList = new String[size];  // string array

        for (int i = 0; i < wordsList.length; i++) {  //gathers string input
            wordsList[i] = scnr.nextLine();
        }

        for (int i = 0; i < listSize; i++) {
            String currWord = wordsList[i];
            int freqCount = getFrequencyOfWord(wordsList, listSize, currWord);
            System.out.println(currWord + " " + freqCount);
        }
    }

}    
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
Jake
  • 13
  • 1
  • 5
  • https://stackoverflow.com/questions/29618205/counting-an-occurrence-in-an-array-java – Kaus2b Oct 28 '21 at 01:26
  • This would be using strings. – Jake Oct 28 '21 at 01:28
  • same idea. Instead of `==` use `.equals()` – Kaus2b Oct 28 '21 at 01:33
  • I"m confused, I went to this thread and an answer wasn't marked in the question. The strings are stored. I need to be able to store the frequencies into another array. That doesn't answer my question. I would need to create another array but I want to be able to check the string array and update each array entry with the frequency of times the string repeats. – Jake Oct 28 '21 at 01:40

1 Answers1

-1
int some_method(String[] arr, String word) {
    int count = 0;
    for (int i=0; i<arr.size(); i++) {
        if (arr[i].equals(word)) count++;
    }
    return count;
}

Then in main method:

String[] array = ["joe", "jake", "jim", "joe"] //or take from user input

int[] countArray = new int[array.size()]

for (int i=0; i<array.size(); i++) {
    countArray[i] = some_method(array, array[i])    
}

System.out.println(array[0] + " " + countArray[0]);

Ouput: joe 2

Kaus2b
  • 747
  • 4
  • 12