Try / Catch Exception未按计划运行

时间:2015-11-19 01:08:23

标签: java exception try-catch helper

我对此感到困惑 - 我为辅助方法写了一个try / catch。它的目的是捕获任何无效的输入(任何不是“男性”或“女性”(没有具体情况)。如果输入无效,它将通知用户,然后让他们再试一次。如果有效,则方法将返回输入。

当我运行程序时,它不会捕获无效输入。为什么这不起作用?

这是辅助方法:

//Helper method that gathers the string input from user
public static String getString() {

    //Create new scanner input
    Scanner input = new Scanner(System.in);

    //Declare and initialize variables
    String wordIn = "";
    boolean validIn;

    //do/while loop for obtaining input and checking validity (try/catch)
    do {
        validIn = true;
        try{
            wordIn = input.nextLine();

        //End Try   
        }

        catch(Exception invalidInput) {
            validIn = false;
            input = new Scanner(System.in);
            System.out.print("\nYou have entered an invalid input. Please "
                    + "enter either \"Male\" or \"Female\" ONLY for this "
                    + "selection. The selection is not case sensitive."
                    + "\n\nPlease enter your selection: ");

        //End Catch    
        }

    //While input is valid, return the input    
    } while (!validIn);
     return wordIn;

//End helper
}

以下是测试中的代码:

//Obtain user input and print output
String passGender = getString();
System.out.println("\n" + titanic.getSurvivedPassengersGender(passGender)
                        + " " + passGender.toLowerCase() + " passengers survived the "
                        + "sinking of the Titanic.");

好像我没有条件设置......我找不到我错的地方。我还是新手,所以我确定这是一个简单的错误。任何帮助是极大的赞赏。谢谢大家!

2 个答案:

答案 0 :(得分:3)

您没有为自己想要的内容设置任何条件。 你没有为输入不是“男性”或“女性”的事件设置任何条件。你的代码应该是:

//Helper method that gathers the string input from user
public static String getString() {

//Create new scanner input
Scanner input = new Scanner(System.in);

//Declare and initialize variables
String wordIn = "";
boolean validIn;

//do/while loop for obtaining input and checking validity (try/catch)
do {
    validIn = true;
    try{
        wordIn = input.nextLine();
        if(!(wordIn.equalsIgnoreCase("male") || wordIn.equalsIgnoreCase("female")))
            throw new Exception();
    //End Try   
    }

    catch(Exception invalidInput) {
        validIn = false;
        input = new Scanner(System.in);
        System.out.print("\nYou have entered an invalid input. Please "
                + "enter either \"Male\" or \"Female\" ONLY for this "
                + "selection. The selection is not case sensitive."
                + "\n\nPlease enter your selection: ");

    //End Catch    
    }

//While input is valid, return the input    
} while (!validIn);
 return wordIn;
//End helper
}

编辑另外,就像@ashutosh说的那样,你不必抛出异常,你可以只使用一个条件:

wordIn = input.nextLine();
if(!(wordIn.equalsIgnoreCase("male") || wordIn.equalsIgnoreCase("female"))
     System.out.print("\nYou have entered an invalid input. Please "
                + "enter either \"Male\" or \"Female\" ONLY for this "
                + "selection. The selection is not case sensitive."
                + "\n\nPlease enter your selection: ");

答案 1 :(得分:1)

或者(正如许多人在评论中所说的那样)你应该更好地使用条件。类似的东西:

wordIn = input.next(); 

while(!(wordIn.toLowerCase().equals("male") || wordIn.toLowerCase().equals("female"))){

System.out.println("\nYou have entered an invalid input. Please "
                + "enter either \"Male\" or \"Female\" ONLY for this "
                + "selection. The selection is not case sensitive."
                + "\n\nPlease enter your selection: ");
wordIn = input.next();

}

这将保持循环,直到输入有效字符串而不需要try catch。