Determine if next token is an int?

Page 1 of 1

1 Replies - 3340 Views - Last Post: 05 April 2013 - 08:19 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=317764&amp;s=20b7c201fa34dfe04a45970a37412ae4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 hycfxduisknbfhjr

Reputation: 0
• Posts: 15
• Joined: 10-March 13

Determine if next token is an int?

Posted 05 April 2013 - 06:48 PM

I need to have this while loop add characters from a file to a string until it finds the first integer in the file. Once it finds an integer, it needs to break out the while loop and continue on to the next function of the program. This is what I have tried but it does not break out of the while loop until it read in '-'. I'm not sure why this is happening or any other way to obtain the functionality that I need.

``` String a = new String();

while (scan.hasNext()){

if (scan.hasNextInt()) break;

else a = a + scan.next();
}

```

I have also tried this:

``` String a = new String();
while (scan.hasNext()){

if (scan.nextInt() < numberOfStates) break;

else a = a + scan.next();
}

```

In this case numberOfStates is just an integer that has been previously defined.

Is This A Good Question/Topic? 0

Replies To: Determine if next token is an int?

#2 farrell2k

• D.I.C Lover

Reputation: 874
• Posts: 2,706
• Joined: 29-July 11

Re: Determine if next token is an int?

Posted 05 April 2013 - 08:19 PM

Read everything as a String. Try to parse each input to an int. If it succeeds, quit the loop. If it fails, continue.