java.util.NoSuchElementException: No line found, do I need to handle this exception? -
my program compiles, getting run time exceptions, not sure if need handle them? methods commented out , extended class, display text. don't know if using labels correctly? want program loop , handle exceptions erroneous characters, does, should terminate if user enters "5".
import java.util.*; import java.util.inputmismatchexception; import java.util.scanner; public class mainmenu3 { // extends premierleagueclubs public static void main(string args[]){ boolean shouldexit = false; int option = 0; loop: while (!shouldexit) { try{ scanner in = new scanner(system.in); menu(); system.out.println("\n"); option = in.nextint(); } // end try catch(inputmismatchexception e) { string option2 = integer.tostring(option); } // end catch switch (option) { case 1: chooseteam(); break; case 2: createprofile(); break; case 3: loadsave(); break; case 4: credits(); break; case 5: break loop; default: system.out.println("invalid choice"); } } // 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); } // 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
there multiple issues program:
- you define shouldexit update according user input
- you scan , process user input both in main() , in menu(). generally, code isn't structured in coherent readable manner , there little reuse.
println()prints new line (as opposedprint())- for love of programming goddess - do not use system.exit() terminate java program
here example of working class above guidelines implemented pasable degree :
import java.util.*; public class mainmenu3 { static scanner in; public static void main(string args[]) { in = new scanner(system.in); boolean shouldexit = false; while (!shouldexit) { displaymenu(); shouldexit = processuserinput(); } } public static void displaymenu() { system.out.println(); system.out.println("created darren estcourt"); system.out.println("please choose option : "); system.out.println("1. choose team"); system.out.println("2. create profile"); system.out.println("3. load/save game"); system.out.println("4. credits"); system.out.println("5. quit"); } public static int getuserinput() { int option = -1; try { option = in.nextint(); } catch (inputmismatchexception e) { // when scanner throws inputmismatchexception, // scanner not pass token caused exception, // may retrieved or skipped via other method. in.next(); } catch (nosuchelementexception e) { // input exhausted option = 5; } catch (illegalstateexception e) {} // scanner closed option = 5; } return option; } public static boolean processuserinput() { int option = getuserinput(); switch (option) { case 1: chooseteam(); break; case 2: createprofile(); break; case 3: loadsave(); break; case 4: credits(); break; case 5: system.out.println("goodbye!"); return true; default: system.out.println("invalid choice"); } return false; } public static void chooseteam() { system.out.println(); 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 = getuserinput(); system.out.println("you entered : " + option); } // end chooseteam public static void createprofile() { system.out.println("createprofile"); } public static void loadsave() { system.out.println("loadsave"); } public static void credits() { system.out.println("credits"); } }
Comments
Post a Comment