java.util.NoSuchElementException:找不到行,我是否需要处理此异常?

时间:2015-07-08 12:24:31

标签: java

我的程序编译,但我得到运行时异常,不确定我是否需要处理它们?注释掉的方法和扩展类,它们只显示文本。我也不知道我是否正确使用标签?我希望我的程序循环并处理错误字符的异常,但它只应在用户输入“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 by Darren Estcourt");   
    System.out.println("\n");
    System.out.println("Please choose an 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 an option between 1 and 4");
//menu();
    } // end switch



} // end menu




} // end class

1 个答案:

答案 0 :(得分:0)

您的计划存在多个问题:

  1. 您定义了shouldExit但是根据用户输入更新它
  2. 您可以在main()和menu()中扫描和处理用户输入。通常,您的代码不是以连贯的可读方式构建的,并且几乎没有重用。
  3. println()已经打印了一个新行(而不是print()
  4. 对编程女神的热爱 - 不使用System.exit()来终止Java程序
  5. 这是一个工人阶级的例子,上面的指导方针是以适用的程度实施的:

    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 by Darren Estcourt");
            System.out.println("Please choose an 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 a scanner throws an InputMismatchException, 
                // the scanner will not pass the token that caused the exception,
                // so that it may be retrieved or skipped via some other method.
                in.next();
            } catch (NoSuchElementException e) {
                // input is exhausted
                option = 5;
            } catch (IllegalStateException e) {}
                // scanner is 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");
        }
    }