java - Try - Catch in if - else if instruction -


i able implement function of try - catch variable choice , works great. have problem variable stopnie. want check if numerical value. tried throw in try catch, unfortunately without success

class task {  public static void main(string[] args) {     scanner user_input = new scanner (system.in);     system.out.println("pick 1 convert fahrenheit celsius");     system.out.println("pick 2 convert ceslius fahrenheit");     int choice = 0;     double stopnie = 0.0;     double convert = 0.0;     decimalformat df = new decimalformat("0.00", new decimalformatsymbols(locale.us));     boolean loop = true;  while (loop)  {     try      {         choice = user_input.nextint();         loop = false;     }      catch (exception e)      {         system.out.println("bad value");         system.out.println("try again");         user_input.next();      } }      if(choice == 1)      {          system.out.println("let me know celsius value");         stopnie = user_input.nextdouble();         convert = stopnie/1.8-35;         system.out.println(stopnie + " c " + " = " + df.format(convert) + " f");     }      else if (choice == 2)      {         system.out.println("let me know fahrenheit value");         stopnie = user_input.nextdouble();         convert = stopnie*1.8+35;         system.out.println(stopnie + " f " + " = " + convert + " c");      }      else      {         system.out.println("bad value");     }  }     } 

so, added try catch if(choice == 1): while loop

    if(choice == 1)      {          while (loop)          {         try {             system.out.println("let me know celsius value");             stopnie = user_input.nextdouble();             convert = stopnie/1.8-35;             system.out.println(stopnie + " c " + " = " + df.format(convert) + " f");             } catch (exception e) {                 system.out.println("bad value");                 system.out.println("try again");                 user_input.next();             }         }     } 

now, when start program , pick 1 nothing happens. want pick 1, go function if(chooice ==1) , if there error print bad value, try again , add input put value again

try code:

public static void main(string[] args) {         scanner user_input = new scanner (system.in);         try {             int f=user_input.nextint();             system.out.println("it's integer");         } catch (inputmismatchexception e) {             system.out.println("it's not integer");             // should print exception            //e.printstacktrace();          }      } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -