3

How can I say the following:

while(input is not an int){
do this
}

I tried this code but I know it's wrong:

 int identificationnumber;
 Scanner sc3 = new Scanner(System.in);
identificationnumber = sc3.nextInt();

while( identificationnumber != int){ // this line is wrong 

Scanner sc4 = new Scanner(System.in);
identificationnumber = sc4.nextInt();

}

Any suggestions please.Thanks.

mjv
  • 67,473
  • 12
  • 100
  • 152
Adegoke A
  • 949
  • 5
  • 26
  • 50

7 Answers7

6

Javadocs are your friend: http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html

nextInt() throws an exception if the next token isn't an int. You're probably looking for hasNextInt()

Also, why are you creating a new Scanner every time you loop? (Or at all - you already have one before the loop)

Brian Roach
  • 72,790
  • 10
  • 128
  • 154
6

try :

while (! scanner.hasNextInt()) { // while the next token is not an int...
    scanner.next();              // just skip it
}
int i = scanner.nextInt();       // then read the int
Olivier Croisier
  • 5,881
  • 23
  • 32
1

Scanner throws an exception before getting to that line http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextInt()

The following code will work:

    int i = 0;
    while(true){
        Scanner scan = new Scanner(System.in);
        try{
            i = scan.nextInt();
        }catch (Exception e) {
            break;
        }
    }
Alin Stoian
  • 1,062
  • 1
  • 10
  • 24
1

You want this?

String identificationnumber;
Scanner scanner = new Scanner(System.in);//Only one Scanner is needed

while (scanner.hasNext()) { // Is there has next input?
    identificationnumber = scanner.next();//Get next input
    try {
        Integer.parseInt(identificationnumber);//Try to parse to integer
        System.out.println(identificationnumber + " is a number!");
    } catch (NumberFormatException e) {
        System.out.println(identificationnumber + " is not a number!");
    }
}
plucury
  • 1,040
  • 1
  • 8
  • 16
  • Thanks for this - but it just leaves me in an infinite loop. Is there a way I can do this with a for loop? – Adegoke A Feb 09 '12 at 10:56
  • @ayokunleadeosun If you want break the loop you can define a end flag string or just use "control + c" – plucury Feb 09 '12 at 13:41
0

By writing sc3.nextInt() I assume you always get an int back, so checking for a non int seems a bit strange.

Maybe its better to return a string with the number inside. If the string is empty stop (you can just check against "") and otherwise convert it to an integer.

Michel Keijzers
  • 13,956
  • 24
  • 83
  • 111
0

Use nextInt() method of scanner class.

It throws,

InputMismatchException - if the next token does not match the Integer regular expression, or is out of range

RanRag
  • 43,987
  • 34
  • 102
  • 155
0

You should be doing this:

if (sc3.hasNextInt())

Check this out: How to use Scanner to accept only valid int as input

With regards to class/type comparison, read this: What is the difference between instanceof and Class.isAssignableFrom(...)?

Community
  • 1
  • 1
caleb
  • 1,449
  • 12
  • 10