java - How to validate whether or not a single input line is only integers? -
the program supposed receive 4 integers in single input user (eg 1 2 3 42). trying write code check whether or not input integers.
however, when input 1 2 b, not enter while loop, , can't figure out why. appreciated.
scanner scan = new scanner(system.in); system.out.print("please list @ least 1 , 10 integers: "); scan.hasnextint(); while(!scan.hasnextint()) { system.out.println("one or more of inputs not integer. please input integers: "); scan.next(); }
you not progressing reading next int
scanner
.
try input 1 b
using following code:
scan.hasnextint(); scan.nextint(); // or scan.next() read next integer while(!scan.hasnextint()) { system.out.println("one or more of inputs not integer. please input integers: "); scan.next(); }
it print:
one or more of inputs not integer. please input integers:
only integers:one or more of inputs not integer. please input
Comments
Post a Comment