Java用户输入错误处理

Java用户输入错误处理,java,error-handling,user-input,Java,Error Handling,User Input,我有一个简单的java程序,它接受integer、double和string类型的3个用户输入。我想知道对所有这些输入执行错误处理的最佳/最有效的方法,以保持程序运行,通知用户他们输入了错误的输入,并再次向他们提问。任何帮助都将不胜感激 这是我的密码 Scanner scan = new Scanner(System.in); int inputInt; double inputDbl; String inputString; System.out.p

我有一个简单的java程序,它接受integer、double和string类型的3个用户输入。我想知道对所有这些输入执行错误处理的最佳/最有效的方法,以保持程序运行,通知用户他们输入了错误的输入,并再次向他们提问。任何帮助都将不胜感激

这是我的密码

    Scanner scan = new Scanner(System.in);
    int inputInt;
    double inputDbl;
    String inputString;


    System.out.print("Please enter a whole number: ");
    inputInt = scan.nextInt();      
    System.out.print("Please enter a decimal number: ");
    inputDbl = scan.nextDouble();
    System.out.print("Please enter a string: ");
    inputString = scan.next().toLowerCase();    
如果要对每个输入分别进行验证,则需要在循环时写入
三次


如果要对每个输入分别进行验证,您需要在循环3次时写入

将其拆分为
n
方法,其中
n
是有多少用户输入

为每个用户输入创建一个获取输入的方法:

String getStringInput(){
    System.out.println("Enter input");
    String input = scan.next();

    //check the input to make sure it is correct
    if(input.equals("foo")){
        //if the input is incorrect tell the user and get the new input
        System.out.println("Invalid Input");
        //simply return this method if the input is incorrect.
        return getStringInput();
    }

    //return the input if it is correct
    return input;
}
对于获取输入的主方法,只需调用该方法:

void getAll(){
    String stringValue = getStringInput();
}

现在可以很容易地获取任意数量的输入,并检查输入是否正确。

将其拆分为
n
方法,其中
n
是有多少用户输入

为每个用户输入创建一个获取输入的方法:

String getStringInput(){
    System.out.println("Enter input");
    String input = scan.next();

    //check the input to make sure it is correct
    if(input.equals("foo")){
        //if the input is incorrect tell the user and get the new input
        System.out.println("Invalid Input");
        //simply return this method if the input is incorrect.
        return getStringInput();
    }

    //return the input if it is correct
    return input;
}
对于获取输入的主方法,只需调用该方法:

void getAll(){
    String stringValue = getStringInput();
}

现在可以很容易地获取任意数量的输入,并检查输入是否正确。

感谢所有输入人员,你们真是太棒了。我选择只使用一个使用布尔触发器的简单dowhile循环。我以前尝试过使用try-catch,但最终编写了大量代码块来执行非常基本的输入检查。所以这里没有任何异常处理,我希望这种方式没有必要。希望它不会让我心碎

do {
        System.out.print("Please enter a whole number: ");
        if (scan.hasNextInt()){     
        inputInt = scan.nextInt();
        validInput = true;
        } else
            System.out.println("You have entered incorrect input! Please enter a whole number only");
            scan.nextLine();
        } while (validInput == false);  

        validInput = false;

        do {
        System.out.print("Please enter a decimal number: ");
        ......
        ......

谢谢大家的支持,你们真是太棒了。我选择只使用一个使用布尔触发器的简单dowhile循环。我以前尝试过使用try-catch,但最终编写了大量代码块来执行非常基本的输入检查。所以这里没有任何异常处理,我希望这种方式没有必要。希望它不会让我心碎

do {
        System.out.print("Please enter a whole number: ");
        if (scan.hasNextInt()){     
        inputInt = scan.nextInt();
        validInput = true;
        } else
            System.out.println("You have entered incorrect input! Please enter a whole number only");
            scan.nextLine();
        } while (validInput == false);  

        validInput = false;

        do {
        System.out.print("Please enter a decimal number: ");
        ......
        ......

在调用
nextInt()
nextDouble()
之前,请确保使用scanner的
hasnextend()
hasNextDouble()方法。不要忘记接受解决问题的答案。确保使用
hasnextend()
hasNextDouble()
调用
nextInt()
nextDouble()
之前的扫描仪方法。不要忘记接受解决问题的答案。