Sunday 15 January 2012

java - using try-catch, how can i recall the try again if the input was wrong? -



java - using try-catch, how can i recall the try again if the input was wrong? -

am building bookstore scheme 1 of methods inquire user come in book name description book's code. question is, how can recall seek if there "inputmismatchexception". mean grab print secreen "wrong input!" want prompt user input again

here code

public void add(){ try{ system.out.println("enter book code :"); int c = s.nextint(); try{ system.out.println("eneter book quantity"); int q = s.nextint(); try{ system.out.println("eneter book description"); string d = s.next(); try{ system.out.println("eneter book cost price"); double cp = s.nextdouble(); try{ system.out.println("eneter book selling price"); double sp = s.nextdouble(); try{ system.out.println("enter status: [a available - u unvailable]"); string input = s.next(); if(input.equals("a")) { system.out.println("available: "); } else if (input.equals("u")) { system.out.println("unavailable "); } else { system.out.println("wrong input"); } try{ system.out.println("enter discount on item \"if exsist\""); double dis = s.nextdouble(); }catch(inputmismatchexception e){ system.out.println("wrong input"); } }catch(inputmismatchexception e){ system.out.println("wrong input"); } }catch(inputmismatchexception e){ } }catch(inputmismatchexception e){ system.out.println("wront input!!"); } }catch(inputmismatchexception e){ system.out.println("wrong input!!"); } } catch(inputmismatchexception e){ system.out.println("wrong input. numbers only!!"); } } catch(inputmismatchexception e){ system.out.println("you have come in number only"); }

you'll need loop of sort, either while or do-while.

boolean invalidinput; { invalidinput = false; seek { system.out.println("enter book quantity"); int q = s.nextint(); } grab (inputmismatchexception e) { system.out.println("please come in valid integer."); // more polite invalidinput = true; // programme loop s.nextline(); } } while (invalidinput);

you don't need nest try/catch blocks if this; perform 1 input until it's valid, next one, etc.

as 1 commenter said, best set each input in method. if this, don't need boolean:

private int getbookquantity() { while (true) { seek { system.out.println("enter book quantity"); int q = s.nextint(); homecoming q; } grab (inputmismatchexception e) { system.out.println("please come in valid integer."); s.nextline(); } } }

which "infinite loop" exits when return statement returns valid value. improve might write 1 method inputting int values (and others double, string):

private int getinteger(string whattoenter) { while (true) { seek { system.out.println("enter " + whattoenter); int q = s.nextint(); homecoming q; } grab (inputmismatchexception e) { system.out.println("please come in valid integer."); s.nextline(); } } }

edit: sorry, forgot nextline() needed after invalid input; throws out invalid integer , else on line won't cause exception repeatedly.

java

No comments:

Post a Comment