java - input a number on scanner -


i trying create simple java program user should input age. if user entered example letter instead of number, message.
in addition message user should asked input , input checked again see if number.
can know how can achieve that?

system.out.println("2 - set age"); scanner b = new scanner(system.in);  if (b.hasnextdouble()) {     double lage = b.nextdouble();     setage(lage);     addemployeemenu(); } else {     system.out.println("you should type numbers!"); } 

you can use while loop

scanner b = new scanner(system.in); double lage;  while (true) {     system.out.println("2 - set age");     if(b.hasnextdouble()){         lage = b.nextdouble();         break;     }else b.nextline(); } 

the point is, number , check inside while loop, repeat long input not correct


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -