Why is StringTokenizer giving different outputs when used in a while loop and a for loop?

Neminda Prabhashwara :

I used StringTokenizer to get the tokens of a string. But I get two different outputs when I tried to print all the tokens in that StringTokenizer, using a for-loop and a while-loop.

String string="She is an attractive girl, isn't she?";
StringTokenizer stringTokenizer=new StringTokenizer(string,",");

when I tried to print all the tokens using a for loop

for (int i=0;i<stringTokenizer.countTokens();i++)
  System.out.println(stringTokenizer.nextToken());

output

She is an attractive girl

when I tried to print all the tokens using a while loop

while (stringTokenizer.hasMoreElements())
  System.out.println(stringTokenizer.nextToken());

output

She is an attractive girl

isn't she?

I want to know why the while loop gives the expected two tokens and the for loop doesn't gives the two tokens.

Progman :

The countTokens() value is decreasing as you call nextToken(). In the first run countTokens() is returning 2 for the two entries found. When you call nextToken() the first time this number is decreased to 1 since there is only one entry left to read. But this means that the for loop will result in

for (int i=0; i<1; i++)

And the for loop variable i is already at value 1. So, since 1<1 is false your for loop terminates without reading the second entry.

You can save the count of tokens in a variable first and then use it in your for loop.

int count = stringTokenizer.countTokens();
for (int i=0; i<count; i++) {
    System.out.println(stringTokenizer.nextToken());
}

This way the countTokens() method gets called only once (with the correct value) and not after every nextToken() call with decreasing remaining token count.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=360036&siteId=1