try/catch java的问题

try/catch java的问题,java,try-catch,message,Java,Try Catch,Message,我在使用此try/catch语句时遇到问题。我试图用它来抛出一条错误消息,如果用户输入一个字母,“请输入一个整数!”。我使用的第一个方法是有效的,但它最终需要两行用户输入,而不是一行,所以它基本上跳过了一个应该被问的问题。在那之后,其他的都不起作用了,它们只是被完全跳过了。我还需要对用户输入做同样的事情,如果用户输入一个字母应该是的整数,它会抛出一条错误消息,说“请输入一个字符串!”。我知道我很接近 public static void main(String[] args) {

我在使用此try/catch语句时遇到问题。我试图用它来抛出一条错误消息,如果用户输入一个字母,“请输入一个整数!”。我使用的第一个方法是有效的,但它最终需要两行用户输入,而不是一行,所以它基本上跳过了一个应该被问的问题。在那之后,其他的都不起作用了,它们只是被完全跳过了。我还需要对用户输入做同样的事情,如果用户输入一个字母应该是的整数,它会抛出一条错误消息,说“请输入一个字符串!”。我知道我很接近

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        boolean validInput = false;
        int val = 0;


        System.out.print("Enter your first name: ");    
        String firstName = input.nextLine();
        System.out.print("Enter your last name: ");
        String lastName = input.nextLine();
        System.out.print("Enter your address: ");
        String address = input.nextLine();
        System.out.print("Enter your city: ");
        String city = input.nextLine();
        System.out.print("Enter your state: ");
        String state = input.nextLine();
        System.out.print("Enter your zip code + 4: ");
        String zip = input.nextLine();
        while(!validInput) {
             try {
        val = input.nextInt();
        validInput = true;
           } catch(Exception e) {
        System.out.println("Please enter an integer!");
        input.nextLine();
    }
}
        System.out.print("Enter amount owed: ");
        String amount = input.nextLine();
        while(!validInput) {
             try {
        val = input.nextInt();
        validInput = true;
           } catch(Exception e) {
        System.out.println("Please enter an integer!");
        input.next();
    }
}
        System.out.print("Enter your payment amount: ");
        String payment = input.nextLine();
        while(!validInput) {
             try {
        val = input.nextInt();
        validInput = true;
           } catch(Exception e) {
        System.out.println("Please enter an integer!");
        input.next();
    }
}
        System.out.print("Enter the date of payment: ");
        String date = input.nextLine();
        while(!validInput) {
             try {
        val = input.nextInt();
        validInput = true;
           } catch(Exception e) {
        System.out.println("Please enter an integer!");
        input.next();
    }
}
        System.out.println("\t\t\t\t\t" + "XYZ Hospital");
        System.out.println();
        System.out.println("Name Information" + "\t\t\t\t" + "Address" + "\t\t\t\t\t\t" + "Payment");
        System.out.println();
        System.out.println("Last" +"\t"+ "First" + "\t\t\t" + "Address Line 1" + "\t" + "City" + "\t" + "State" + "\t" + "Zip" + "\t" + "Amount Owed" + "\t" + "Payment Amount" + "\t" + "Payment Date");
        System.out.println();
        System.out.println(lastName + " " + firstName + "\t" + address + " " + city + ", " + state + " " + zip + "\t" + amount + "\t\t" + payment +"\t\t"+ date);
        System.out.print("Would you like to enter abother patient? Type Yes or No: ");
        String userInput = input.nextLine();
        if (userInput.equals("Yes")) {
            while (userInput.equals("Yes")) {
                System.out.print("Enter your first name: ");    
                String firstName2 = input.nextLine();
                System.out.print("Enter your last name: ");
                String lastName2 = input.nextLine();
                System.out.print("Enter your address: ");
                String address2 = input.nextLine();
                System.out.print("Enter your city: ");
                String city2 = input.nextLine();
                System.out.print("Enter your state: ");
                String state2 = input.nextLine();
                System.out.print("Enter your zip code + 4: ");
                String zip2 = input.nextLine();
                System.out.print("Enter amount owed: ");
                String amount2 = input.nextLine();
                System.out.print("Enter your payment amount: ");
                String payment2 = input.nextLine();
                System.out.print("Enter the date of payment: ");
                String date2 = input.nextLine();
                System.out.println("\t\t\t\t\t" + "XYZ Hospital");
                System.out.println();
                System.out.println("Name Information" + "\t\t\t\t" + "Address" + "\t\t\t\t\t\t" + "Payment");
                System.out.println();
                System.out.println("Last" +"\t"+ "First" + "\t\t\t" + "Address Line 1" + "\t" + "City" + "\t" + "State" + "\t" + "Zip" + "\t" + "Amount Owed" + "\t" + "Payment Amount" + "\t" + "Payment Date");
                System.out.println();
                System.out.println(lastName2 + " " + firstName2 + "\t" + address2 + " " + city2 + ", " + state2 + " " + zip2 + "\t" + amount2 + "\t\t" + payment2 +"\t\t"+ date2);
                System.out.print("Would you like to enter another patient? Type Yes or No: ");
                userInput = input.nextLine();
            }

        }
        else if (userInput.equals("No")) {
            System.out.println("Goodbye");
        }

在第一次while循环后,不会将validInput重置为False。因此它不会进入下一个。

您应该在catch块comment
input.nextLine()
中获取输入,然后它应该正常工作

我在代码中添加了解释

public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  boolean validInput = false;
  int val = 0;

  System.out.print("Enter your first name: ");
  String firstName = input.nextLine();
  System.out.print("Enter your last name: ");
  String lastName = input.nextLine();
  System.out.print("Enter your address: ");
  String address = input.nextLine();
  System.out.print("Enter your city: ");
  String city = input.nextLine();
  System.out.print("Enter your state: ");
  String state = input.nextLine();
  System.out.print("Enter your zip code + 4: ");
  String zip = input.nextLine();
  while (!validInput) {
    try {
      val = input.nextInt();
      validInput = true; //if exception occurs this line wont be executed
    } catch (Exception e) {
      System.out.println("Please enter an integer!");
      // input.nextLine(); --remove this line
    }
  }
  System.out.print("Enter amount owed: ");
  String amount = input.nextLine();
  //reset the value of validInput
  //validInput will true after the execution of above while loop
  validInput = false;
  while (!validInput) {
    try {
      val = input.nextInt();
      validInput = true; //if exception occurs this line wont be executed
    } catch (Exception e) {
      System.out.println("Please enter an integer!");
      // input.next(); -- remove this line
    }
  }
}

可能是一个快速的解决方案,我使用了
Scanner.hasNextInt
在获取所需INT之前检查下一个令牌是什么:

import java.util.Scanner;

public class java_so {
    private static int privateGetInt(String request, Scanner input) {
        // cant be false, but we return when done.
        while (true) {
            // print the question
            System.out.println(request);
            // check what the next token is, if an int we can then retrieve
            if (input.hasNextInt() == true) {
                int out = input.nextInt();
                input.nextLine(); // clear line, as nextInt is token orientated, not line,
                return out;
            } else {
                // else for when not an int, clear line and try again
                input.nextLine();
            }
        }
    }

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

            System.out.print("Enter your first name: ");    
            System.out.println(input.nextLine());
            // call function above 
            System.out.println(privateGetInt("enter an Int", input));
            System.out.print("Enter your last name: ");
            System.out.println(input.nextLine());
            System.out.print("Enter your address: ");
            System.out.println(input.nextLine());
    }
}

这似乎有点像问题可能是在调用了
input.nextInt
之后出现的。您还没有摆脱该行的其余部分(可能只是
“\n”
),因此下一次调用
input.getLine()
只会得到它,跳到其中一个。使用
input.getInt
input.getLine
可以清除此问题。

看看如何发布。另外,请正确格式化您的代码。您需要在每个循环后设置
validInput
的默认值。这并不意味着';这似乎不是最好的做事方式。您假设用户输入了不正确的zip值,并且您总是要求输入两次。每次循环后,应将
val
保存到相应的变量中。有一个hasNextInt函数用于检查输入是否为int,可能有用