How to handle user input type in java? -
if allowed input in program of type int
user arbitrarily types in not-allowed type character such double
or char
etc. how handle that? sure there sort of throw exception
sort of thing not sure how done. suggestions appreciated
//here code system.out.print("too " +additionalinfo+ "! try again: "); attemptcounter++; int x= input.nextint(); // here want usr type in input
use loop prompt user input again after invalid input e.g.
int userinput; while(true) { try { system.out.println("please enter number: "); scanner input = new scanner(system.in); userinput = input.nextint(); break; } catch(inputmismatchexception | numberformatexception ex ) { system.out.println("invalid number, please try again"); } }
Comments
Post a Comment