Reputation: 434
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.
Upvotes: 5
Views: 333
Reputation: 19554
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.
Upvotes: 6