dimanche 16 mai 2021

I am trying to make a word counter but it seems that something is wrong with my logic as there are no syntax errors but it fails to work properly

In this code I used two Strings and an ArrayList. User input is taken and stored as a String(text) and then the .length() method is used to used to iterate through the for loop. In the for loop there is an if statement. The if statement is supposed to concatenate each character in the first String(text) into the second String(word) until either a dot or space is met. The else portion of the statement then adds the current string(word) to the ArrayList and sets the String as "" to clear it for the next loop. When the loop is finally over the .size() of the ArrayList should be printed. This seemed to work at first but then simply putting a String of dots or spaces would count as words and not adding anything but two words and a space would give me a word count of one, I think the program is only counting the dots or spaces as words but this is the exact opposite of what I wanted it to do. I am not sure how to fix this.The code Seems to work here Counting spaces as words Counting dots as words

Aucun commentaire:

Enregistrer un commentaire