In summary, this code is a whitespace delimeted tokenizer that splits the string up into up to three pieces.
Hence, in this particular example, the values of s1, s2 and s3 will be:
s1 = "You";
s2 = "are";
s3 = "cool";
To see the values stored in them, just do:
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
Now, as for why?
See this:
String s1, s2, s3="";//these are the strings that will hold the sub tokens
StringTokenizer line = new StringTokenizer("You are cool");//this initializes an object of the StringTokenizer class with a string value of "You are cool"
s1 = line.nextToken();//this reads up until the first whitespace character (which will be skipped)
s2 = line.nextToken();//this will read from the last position of the iterator
//this will continue reading tokens (delimited by whitespace) from the initialized
//StringTokenizer, (now at the position after "are"):
while (line.hasMoreTokens())
s3 +=line.nextToken();//and those tokens are **appended** to s3! Note appended! Not stored in or overwritten to!
Hence, the claim that *this program tokenizes a string up to three times (by whitespace).
But, you should be warned: Because in a case where the StringTokenizer is initialized to this:
"You are cool, bro"
(Note the extra whitespace and characters following the whitespace)
You'll get this:
s1 = "You";
s2 = "are";
s3 = "cool,bro";//note the lack of whitespace!
The last part comes from the fact that in the while loop:
while (line.hasMoreTokens())
s3 +=line.nextToken();//a nextToken() call skips over whitespace by default
Hence, s3 appends the next token from line
, no matter how many there are.