java - how to handle this InputMismatchException? -


in code below i'm getting inputmismatchexception @ line 80 before i'm giving input. why this?

try {     loop:while(true)     {         choice=sc.nextint();         switch (choice) {             case 1:                  term=3;                 break loop;             case 2:                 term=6;                 break loop;             default:                 system.out.println("invalid input.. enter again");                 choice=sc.nextint();         }     }  } catch (inputmismatchexception e2) {     system.out.println("wrong format!! enter number");     choice=sc.nextint();  //line 80 } 

consume end of line:

catch (inputmismatchexception e2) {     system.out.println("wrong format!! enter number");     sc.nextline(); // add     choice=sc.nextint();  //line 80 } 

in addition, shouldn't have 2 choice=sc.nextint(); in loop.

and want put try-catch inside loop, in order stay in loop after exception caught.


Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -