1 Replies - 502 Views - Last Post: 05 April 2013 - 08:19 PM Rate Topic: -----

#1 hycfxduisknbfhjr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1