-2

I'm supposed to read through a file, add all the new words to an arraylist, and if the word is already in the list, increase a counter on how many times it has appeared. I read in the words just fine, but when it comes to adding them to the list, it seems to ignore the part where it checks whether the word is already in the list and adds multiples of the same word. What I've got:

Reading method:

public void read(String text) throws Exception{
  File fileText = new File(text);
  Scanner in = new Scanner(fileText);
  while(in.hasNextLine()){
    newWord = new Word(in.nextLine());
    add(newWord.text);
    }
  }

Method for adding to arraylist

public void add(String text){
  for(Word o: wordList){
    if(wordList.contains(newWord.text){
      newWord.increaseCount();
    }else{
      wordList.add(newWord);  
     }
   }

Would really appreciate some help, I'm completely lost as to where the issue lies....

Telanore
  • 75
  • 1
  • 9
  • 2
    it would be easier to use a map – user140547 Oct 10 '16 at 10:06
  • 1
    Java's collections' "contains" method rely on the "equals" and "hashcode" methods of the type they contain (in your case, Word.java). If those method are not overriden from Object, they won't do "semantic" comparisons (are these two strings the same), but memory reference comparisons (are these 2 pointers the same location). Read more about equals and hashcode and it will make sense. – Diego Martinoia Oct 10 '16 at 10:17
  • BTW: in your add method you iterate through your wordlist and do your contains check + add for every iteration. So if your list is 10 elements big and you call your add method, the new value gets inserted once and then the counter increased by 9 times. (Or it will get inserted 10 times if your Word object doesn't override "equals" and "hashcode" correct). You need to delete the "for(Word o: wordList)" loop. You don't use the "o "Object anyway. So its kind of a pointless iteration. – OH GOD SPIDERS Oct 10 '16 at 11:06
  • Also your add method takes an argument "Sting text" and then does absolutly nothing with it. That cannot be correct. – OH GOD SPIDERS Oct 10 '16 at 11:12

3 Answers3

0

it seems that newWord is an instance of a custom class of you. your wordlist should be a list of Strings instead of this type because you do the checking against the text (which i assume is a string). thatswhy each object is added to list, because you check against the instance-address with the newWord-text. so change wordList.add(newWord); to wordList.add(newWord.text);

hasan
  • 553
  • 1
  • 5
  • 19
0

You cannot structurally modify the list while iterating over it using for-each loop.

Stated in the doc -

A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification

See this -

In Java, can you modify a List while iterating through it?

Community
  • 1
  • 1
Derrick
  • 3,669
  • 5
  • 35
  • 50
  • But then how can I iterate through the list to check if it already contains the object? – Telanore Oct 10 '16 at 10:52
  • @Telanore In my answer i said you cannot modify the list structurally i.e perform addition or removal from the list, of course you can check if the list contains the object or not, but for this you have to override equals and hashcode methods, hope you did this, if not see this - http://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java – Derrick Oct 10 '16 at 11:03
0

I'm guessing, because you didn't include the code for your Word class.

Here's how your add method should be written. You either add to the count or add a new word. I'm also assuming that the case of the words doesn't matter.

public void add(String text){
    for (Word o: wordList) {
        if (o.getWord().equalsIgnoreCase(text) {
            o.increaseCount();
            return;
        }   
    }
    wordList.add(new Word(text.toLowerCase());
}

Your read method just calls the add method.

public void read(String text) throws Exception{
   File fileText = new File(text);
   Scanner in = new Scanner(fileText);
   while (in.hasNextLine()) {
       add(in.nextLine().trim());
   }
   in.close();
}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111