我在使用此try / catch语句时遇到问题。我试图用它来抛出一条错误信息,上面写着“请输入一个整数!”如果用户输入了一封信。我使用的第一个工作,但它最终采取2行用户输入而不是一个,所以它基本上跳过应该被问的问题。在那之后,其他任何一个都没有工作,他们只是完全被跳过。我还需要为用户输入做同样的事情,如果用户输入一个字母应该是的整数,它会抛出一条错误消息“请输入一个字符串!”。我知道我非常接近!
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");
}
答案 0 :(得分:1)
在第一次while循环后,您不会将validInput重置为False。所以它不会进入下一个。
答案 1 :(得分:1)
你应该在catch块中的catch块注释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
}
}
}
答案 2 :(得分:0)
快速解决方案可能是,我在使用Scanner.hasNextInt
之前检查了下一个令牌是什么:
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
清除此内容。