Hi everyone,

I need help with a homework problem. I have to write a code that counts the number of singletons, stored in the variable n, in a string sequence. A singleton is a word in a sequence that does not appear before or after itself in the sequence.

I created a count variable n and initiliazed it to 2, since the first and last tokens can only have one
copy before and after each other respectively. I divided the string sequence into tokens by splitting it with respect to the empty spaces between them.

Here is my code.

Code:
String string = " ";
sequence = stdin.next();
 
String[] tokens = sequence.split(" ");
 
n = 2;
 
for (int i = 1; i < tokens.length()-1; i++){
	while (!(tokens[i].equals("xxxxx"))){ 
		if (!(tokens[i].equals(tokens[i-1]) && !(tokens[i].equals(tokens[i+1])))
			n++;
	}
}
I am trying to figure out what is wrong with my code. Can someone help?