0

What are the values of s1, s2, and s3 after the following code executes?

String s1, s2, s3="";
StringTokenizer line = new StringTokenizer("You are cool");
s1 = line.nextToken();
s2 = line.nextToken();
while (line.hasMoreTokens())
    s3 +=line.nextToken();

Note this is a study guide question which I'm unable to find. If anyone could explain it thoroughly so that I can candle this type of question on the exam, I would appreciate it.

hjg hjg
  • 111
  • 2
  • 3
    The code has no "output" (as in command-line output, since there's no `System.out.println`). Not sure if that helps. See [this](http://ideone.com/JW4gWW) for how to print it. – Bernhard Barker Apr 14 '13 at 02:38

3 Answers3

1

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.

jrd1
  • 10,358
  • 4
  • 34
  • 51
0

String s1 s2 s3 are instantiated as empty not null.

The variable line basically is a new string ("You are cool") ready to be tokenized.

Everytime you do nextToken() it will take a word or token and store it in that varible

So this code will store the first two words.

s1 = line.nextToken();
s2 = line.nextToken();

This code will see if their is more words or tokens, which their are (1 left). It will then take that last token and assign it to s3

while (line.hasMoreTokens()) {
    s3 +=line.nextToken();
}

Output wise, the program is not psychically outputting anything into the console, it is however doing it in memory. This is what it would look like in memory and if you were to output each variable with System.out.println().

s1 = "You"

s2 = "are"

s3 = "cool"

Joban
  • 1,318
  • 7
  • 19
  • So if the parameter for the StringTokenizer was ("To understand the concept in six"), s1 would be the first two words (tokens), s2 the second two words (tokens) and s3 the last three words (tokens), correct? – hjg hjg Apr 14 '13 at 03:00
  • 1
    No `s1` would be the first word and `s2` would be the next word. and `s3` since its in a while loop and has the `+=` operator which means it will add on to it and equal it will be the rest of the sentence. – Joban Apr 14 '13 at 03:02
0

As @Dukeling mentioned,you probably sre no output because you dont print out nothing.

Also, please tak a look on this answer: Why is StringTokenizer deprecated?

From the javadoc for StringTokenizer: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

Community
  • 1
  • 1
evgenyl
  • 7,837
  • 2
  • 27
  • 32