java - How to stop letters and erroneous characters as input -


i want throw error message when char or erroneous character input, loop works numbers other 1-5.
class premierleagueclubs displays text. guessing need try catch block(s), maybe inputmismatchexception, i'm not sure in code put this?

    import java.util.*;     import java.util.inputmismatchexception;     import java.util.scanner;      public class mainmenu3 extends premierleagueclubs{      public static void main(string args[]){             boolean valid = false;             int option = 0;   while (!valid) {         scanner in = new scanner(system.in);              menu();         system.out.println("\n");         option = in.nextint();         valid = option > 0 && option < 6;         switch (option) {             case 1:         chooseteam();          case 2:          createprofile();         //break;          case 3:         loadsave();     //  break;          case 4:         credits();       //  break;          case 5:         system.out.println("goodbye!");      }         } // end switch         } // end main method      public static void chooseteam(){     system.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");     system.out.println("select team : ");     system.out.println("1. arsenal");     system.out.println("2. aston villa");     system.out.println("3. bournemouth");     system.out.println("4. chelsea");     system.out.println("5. crystal palace");     system.out.println("6. everton");     system.out.println("7. leicester city");     system.out.println("8. liverpool");     system.out.println("9. manchester united");     system.out.println("10. manchester city");     system.out.println("11. newcastle united");     system.out.println("12. norwich city");     system.out.println("13. southampton");     system.out.println("14. stoke city");     system.out.println("15. sunderland");     system.out.println("16. swansea city");     system.out.println("17. tottenham hotspur");     system.out.println("18. watford");     system.out.println("19. west brom");     system.out.println("20. west ham united");      int option = 0;          scanner in = new scanner(system.in);         //menu();          system.out.println("\n");         option = in.nextint();         system.out.println("you entered : " + option);          if (option == 1)             arsenal();         else               if (option == 2)         astonvilla();         else          if (option == 3)         bournemouth();         else          if (option == 4)         chelsea();          else          if (option == 5)         crystalpalace();              else         if (option == 6)         everton();          else         if (option == 7)         leicester();          else         if (option == 8)         liverpool();          else         if (option == 9)         manchesterunited();          else         if (option == 10)         manchestercity();          else         if (option == 11)         newcastleunited();          else         if (option == 12)         norwichcity();          else         if (option == 13)         southampton();          else         if (option == 14)         stokecity();          else         if (option == 15)         sunderland();          else         if (option == 16)         swanseacity();          else         if (option == 17)         tottenhamhotspur();          else         if (option == 18)         watford();          else         if (option == 19)         westbrom();          else         if (option == 20)         westhamunited();           } // end chooseteam        public static void createprofile(){     } // end createprofile     public static void loadsave(){     } // end loadsave     public static void credits(){     } // end credits       public static void menu(){        system.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");         system.out.println("created darren estcourt");            system.out.println("\n");         system.out.println("please choose option : ");         system.out.println("\n");         system.out.println("1. choose team");         system.out.println("\n");         system.out.println("2. create profile");         system.out.println("\n");         system.out.println("3. load/save game");         system.out.println("\n");         system.out.println("4. credits");            system.out.println("\n");         system.out.println("5. quit");          system.out.println("\n");          string option="0";            scanner in = new scanner(system.in);          system.out.println("\n");         option = in.nextline();          switch (option) {          case "1":         chooseteam();         break;          case "2":            createprofile();         break;          case "3":         loadsave();         break;          case "4":         credits();           break;          case "5":         system.out.println("goodbye!");           default:         system.out.println("please select option between 1 , 4");     //menu();         } // end switch        } // end menu         } // end class 

throwing exception may not best idea here stop program if not handled properly. sernd suggested, adding default case seems simple solution here

boolean shouldexit = false; while (!shouldexit) {     scanner in = new scanner(system.in);          menu();     system.out.println("\n");     option = in.nextint();     switch (option) {         case 1:           chooseteam();           break;         case 2:            createprofile();           break;         case 3:           loadsave();           break;         case 4:           credits();             break;         case 5:           system.out.println("goodbye!");           shouldexit=true;            break;         default:          system.out.println("invalid selection");     } } 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

python - How to remove the Xframe Options header in django? -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -