+ 1

Please explain what i missed

The problem suddenly happening when i type a not integer symbol, with intention to get InputMismatchException , a next step i waiting is a invitation to the next attempt to type something. But i get a endless cycle instead. ArithmeticException - works as intended. If i'll type 0, i'll got the message "Something but not zero" and then another chance to type something public class TryCatchE { public static void main (String[] args){ int g = 0; boolean flag=true; Scanner prospect = new Scanner(System.in); System.out.println("Type something "); while (flag) { flag=false; try { int in = prospect.nextInt(); g /= in; } catch (InputMismatchException e) { flag = true; System.out.println ("Something Integer"); } catch (ArithmeticException e) { flag=true; System.out.println("Something but not zero"); } } } }

19th Jun 2019, 10:44 PM
Ivanov Viktor
Ivanov Viktor - avatar
1 Réponse
0
Your problem has nothing to do with your code but with Sololearn. On Sololearn you have to enter all input at the beginning. E.g. 0 -> throws an exception -> flag gets true -> loop runs again -> new input expected (error because there is no other input) For such interactive codes you should not use an online compiler.
20th Jun 2019, 12:47 AM
Denise Roßberg
Denise Roßberg - avatar