1

I'm counting the occurrence of a word in a linked list using the Collections.frequency, however because It is in a for loop it reprints the occurrence depending on how many times it finds the word.

For example if i add "test" twice to the list, and print the occurrence it would print

test:2
test:2

Do I need to move it outside the for loop or something?

String word = textField.getText().toLowerCase();
for(String y : wordList) {
   if(y.contains(word)) {
      System.out.println(y + " Occured: " + Collections.frequency(wordList,y) + " times");
    }
}

But I want to keep the use of a LinkedList without a set

  • 1
    Your code is obscure. Why do you use both `String.contains(String)`, which checks for a substring match, and `frequency()`, which scans for an exact match using `String.equals(String)`? If the list contains the strings `{"smart", "fart", "art", "fart"}`, and users enters `"art"`, do you truly want output to be `smart Occured: 1 times`, `fart Occured: 2 times`, `art Occured: 1 times`? – Andreas Oct 30 '17 at 18:39
  • @Andreas Actually I think equalsIgnoreCase should be used since the word "smart" is the same word as "SMART" when it comes to determining if they are the same word – jthort Oct 30 '17 at 18:43
  • @jthort The code does call `toLowerCase()`, so it can be assumed that all the strings in `wordList` are lowercase only. – Andreas Oct 30 '17 at 20:58

2 Answers2

1

You don't need to use a loop. Just using Collections.frequency using the object (word) would be enough:

List<String> wordList = new LinkedList<>();
wordList.add("test");
wordList.add("hello");
wordList.add("test");
wordList.add("world");
wordList.add("test");
wordList.add("hello");
wordList.add("test");
wordList.add("hello");

String word = "test";

System.out.println(word + " Occured: " + Collections.frequency(wordList,word) + " times");

Output:

test Occured: 4 times

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
  • This is only doing a specific word, they want each word that's in the collection – jthort Oct 30 '17 at 18:36
  • @jthort that's not what the OP is asking. – Juan Carlos Mendoza Oct 30 '17 at 18:39
  • You're right, my mistake – jthort Oct 30 '17 at 18:42
  • @Juan Carlos Mendoza Yes that is correct actually. Just a question, I wanted to print the size of the list if the entered string is empty, so `if(word =="") { System.out.println(wordList.size) } but it just prints the `occured: 0`? –  Oct 30 '17 at 18:42
  • 2
    @godlypython never use == when comparing strings in Java. Use the `equals` method instead. [Read this](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) for further information. And the empty string ("") will behave as any other String. So if your list contains the empty string then using the frecuency method will return the number of occurrences of it. – Juan Carlos Mendoza Oct 30 '17 at 18:46
  • Thanks, i'll read up on the differences :) –  Oct 30 '17 at 18:47
0

use count variable and print its value outside the for loop. e.g.

int count=0;
 for(String y : wordList) {
   if(y.contains(word)) {
    count++;
}
 System.out.println(y + " Occured: " + count + " times");
}
Zubair Nabi
  • 1,016
  • 7
  • 28
  • Since you are comparing words, it would be .equalsignorecase since "Hello" = "hello" what talking about the subject words – jthort Oct 30 '17 at 18:31