java - Multiple try catch in a while loop -


hopeing can help. been trawling days , cant find solution. trying create while loop try/throw/catch exception handling, need catch multiple exceptions.

i've tried can think of either doesn't come out loop or skips rest of code (not pasted here) , finishes program.

scanner scanner = new scanner(system.in); boolean notcorrectinput = false; howmanytoadd = 0;  while (!notcorrectinput) {     try {         system.out.println("how many products add?");         howmanytoadd = scanner.nextint();         notcorrectinput = true;         }     catch (inputmismatchexception e){         system.err.println("you have not entered correct number format. please try again.");         }      try {         if (howmanytoadd < 1) {         throw new negativearraysizeexception();         }         }      catch (negativearraysizeexception e) {         system.err.println("you have not entered possitive number. please try again.");         }      }      secondproduct lp[] = new secondproduct[howmanytoadd];  //rest of code here on down. 

i expect int if passed double or float handle in loop , keep going until passed int, if given negative number start off array loop start , ask positive int passed.

you don't need throw exception :

while (!notcorrectinput) {     try {         system.out.println("how many products add?");         howmanytoadd = scanner.nextint();         if (howmanytoadd >= 1)             notcorrectinput = true;         else              system.err.println("you have not entered positive number. please try again.");     }     catch (inputmismatchexception e) {         system.err.println("you have not entered correct number format. please try again.");         scanner.next();     } } 

btw, notcorrectinput confusing name, since set true when input correct.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -