Java 如何在try-and-catch块中进行输入验证?

Java 如何在try-and-catch块中进行输入验证?,java,Java,我有一个程序,用户可以输入他们的名字和东西。它是一个大的尝试支架。最后,当用户输入字母而不是数字时,会出现警告“无效输入”,我想这样做,如果无效的3x,程序将关闭 到目前为止,我有这个。我推荐了一些不必要的代码,但重要的部分只是try和do-while循环 public static void main(String[] args) throws FileNotFoundException { try{ Scanner input = new Scanner(System

我有一个程序,用户可以输入他们的名字和东西。它是一个大的尝试支架。最后,当用户输入字母而不是数字时,会出现警告“无效输入”,我想这样做,如果无效的3x,程序将关闭

到目前为止,我有这个。我推荐了一些不必要的代码,但重要的部分只是try和do-while循环

public static void main(String[] args) throws FileNotFoundException {
    try{
        Scanner input = new Scanner(System.in);

    String input_option = "1";

    // calling option method
        print_options();
        int attempt= 0;
          boolean authenitcated = false;
do{
            input_option = input.nextLine();

        if (input_option.equals("0")) {

            System.out.println("Enter your first name ");
            String firstnameopt0 = input.nextLine();

            System.out.println("Enter your last name ");
            String lastnameopt0 = input.nextLine();

            type.println("Annual Income: " + income);
            type.println("Tax: " + opt0tax);
            myfile.exists();
            type.close();
        }

        else if (input_option.equals("1")) {
            System.out.println("Enter your first name ");
            String firstnameopt1 = input.nextLine();


            type.close();
        }

        else if (input_option.equals("2")) {
            System.out.println("Enter your first name ");
            String firstnameopt2 = input.nextLine();


            myfile.exists();
            type.close();
        }

        //extra_options();


    input.close();
     catch(InputMismatchException  exi){
        System.out.println("you must enter a double");
        attempt++;
    }
}while(attempts < 3 && authenticated == false)
    }
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
试一试{
扫描仪输入=新扫描仪(System.in);
字符串输入\u option=“1”;
//看涨期权法
打印选项();
int尝试=0;
布尔authenitcated=false;
做{
input_option=input.nextLine();
if(输入_选项等于(“0”)){
System.out.println(“输入您的名字”);
字符串firstnameopt0=input.nextLine();
System.out.println(“输入您的姓氏”);
字符串lastnameopt0=input.nextLine();
类型.println(“年收入:”+收入);
类型.println(“税:+opt0tax”);
myfile.exists();
输入.close();
}
else if(输入选项等于(“1”)){
System.out.println(“输入您的名字”);
字符串firstnameopt1=input.nextLine();
输入.close();
}
else if(输入选项等于(“2”)){
System.out.println(“输入您的名字”);
字符串firstnameopt2=input.nextLine();
myfile.exists();
输入.close();
}
//额外选项();
input.close();
捕获(输入不匹配异常exi){
System.out.println(“必须输入双精度”);
尝试++;
}
}while(尝试次数<3&&authenticated==false)
}
  • 您需要在选项“选择零件”的底部添加一个else
  • 好的做法是将其转换为int,因为用户可以使用前导空格输入零,这仍然有效
  • 其次,在catch in finally块之后关闭文件(关闭所有可关闭的资源)

    int尝试=0;
    布尔值=假;
    做{
    input_option=input.nextLine();
    试一试{
    整数选项=整数.valueOf(输入_选项);
    开关(选件){
    案例0:
    System.out.println(“输入您的名字”);
    字符串firstnameopt0=input.nextLine();
    System.out.println(“输入您的姓氏”);
    字符串lastnameopt0=input.nextLine();
    打破
    案例1:
    System.out.println(“输入您的名字”);
    字符串firstnameopt1=input.nextLine();
    打破
    案例2:
    System.out.println(“输入您的名字”);
    字符串firstnameopt2=input.nextLine();
    打破
    违约:
    尝试++;
    System.out.println(“必须输入int”);
    }
    }捕获(例外情况除外){
    System.out.println(“必须输入int”);
    尝试++;
    }
    }while(尝试<3&&authenticated==false);
    

选项选择中缺少else语句,例如inputOption将为4或10Scanner.nextLine始终返回字符串。InputMismatchException(IME)将永远不会被抛出。
  int attempt = 0;
boolean authenticated = false;
do {
    input_option = input.nextLine();
    try {
        Integer option = Integer.valueOf(input_option);
        switch (option) {
            case 0:
                System.out.println("Enter your first name ");
                String firstnameopt0 = input.nextLine();

                System.out.println("Enter your last name ");
                String lastnameopt0 = input.nextLine();
                break;
            case 1:
                System.out.println("Enter your first name ");
                String firstnameopt1 = input.nextLine();
                break;
            case 2:
                System.out.println("Enter your first name ");
                String firstnameopt2 = input.nextLine();
                break;
            default:
                attempt++;
                System.out.println("you must enter a int");
        }

    } catch (Exception ex) {
        System.out.println("you must enter a int");
        attempt++;
    }
} while (attempt < 3 && authenticated == false);