i want re prompt(noofcases) user if entered value not integer. i'm newer java. kindly suggest me best possible ways can re prompt(noofcases) user enter integer value again till user enters integer value.
public static void main(string[] args) { fetchinput(); } public static void fetchinput() { int noofcases = 0; scanner input= new scanner(system.in); noofcases=input.nextint(); arraylist<string> stringa = new arraylist<string>(); arraylist<string> stringb = new arraylist<string>(); (int i=0;i<noofcases;i++) { stringa.add(input.next()); stringb.add(input.next()); } input.close(); system.out.println(stringa); system.out.println(stringb); }
try convert input string integer.parse() in try{}catch(numberformatexception e) , print system out when exception occurs new input request. rest read same input stream again. can in while loop if want or count fails in variable abort.
Comments
Post a Comment