Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 631
Neminda Prabhashwara Avatar asked Mar 26 '20 19:03

Neminda Prabhashwara


People also ask

What does StringTokenizer return?

A token is returned by taking a substring of the string that was used to create the StringTokenizer object. It provides the first step in the parsing process often called lexer or scanner. The String Tokenizer class allows an application to break strings into tokens. It implements the Enumeration interface.

What is the purpose of StringTokenizer?

The string tokenizer class allows an application to break a string into tokens. The tokenization method is much simpler than the one used by the StreamTokenizer class. The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments.

How do I use StringTokenizer with multiple delimiters?

In order to break String into tokens, you need to create a StringTokenizer object and provide a delimiter for splitting strings into tokens. You can pass multiple delimiters e.g. you can break String into tokens by, and: at the same time. If you don't provide any delimiter then by default it will use white-space.


1 Answers

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.

like image 154
Progman Avatar answered Oct 17 '22 08:10

Progman