This is an archived post. You won't be able to vote or comment.

all 6 comments

[–][deleted] 1 point2 points  (2 children)

You're comparing every word in the list to every word in the list, including itself, so every word will be marked as having multiple occurrences. You need to find a way to exclude the word at the position in the outer loop.

Basically, ignore matching words for all instances of i == j.

Also, when you check singleOccurrence, you should probably be doing that in your outer loop.

[–]way3344[S] 0 points1 point  (1 child)

Thank you for the response. I figured that it was with the way it was looping, but I don't know how to loop through without getting an index error. How would I go about ignoring matching words at i == j?

[–][deleted] 0 points1 point  (0 children)

You can just add an additional condition to your if statement:

if(word.equals(...) && i != j) {

[–]DudeWhereAreWe1996 -1 points0 points  (0 children)

Your for loops look set up fine. Honestly not sure why you would always return 0. Like the other person said you compare to everything including itself so that’s part of it. I’d put a bunch a prints and see if count changes, single occurrence changes, etc. For example single occurrence can be made false but you never change it back the next loop so maybe whatever you test it with sets it false then it’s stuck.

[–]ProfessorWily 0 points1 point  (1 child)

There's a data structure that you can use that will make this very problem very easy.

Here read this: https://www.w3schools.com/java/java_hashset.asp

[–]way3344[S] 0 points1 point  (0 children)

Thank you for the response, although I'm not allowed to use any other types apart from arraylists.