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.
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 anint. You’re probably looking forhasNextInt()Also, why are you creating a new
Scannerevery time you loop? (Or at all – you already have one before the loop)