试图使用扫描仪来获取int或字符串..下面的代码就是我所拥有的:
System.out.println("where would you like to send the following item: ");
System.out.println("1. groceries, 2. savings, 3. utilities");
System.out.print("Enter list name or list number to send: ");
String user = user_input.nextLine();
if (user.toLowerCase().equals("groceries") || Integer.parseInt(user) == 1) {
System.out.println("item was sent to the grocery list");
}else if(user.toLowerCase().equals("savings") || Integer.parseInt(user) == 2) {
System.out.println("item was sent to the savings list");
}else if(user.toLowerCase().contains("utilities") || Integer.parseInt(user) == 3) {
System.out.println("item was sent to the utilities list");
}else{
System.out.println("item is not in any category");
}
我的问题是每当我输入一个String时,我都会收到一个NumberFormatException错误。输入int并没有给我任何问题。我认为使用String user = user_input.nextLine();
获取输入并使用Integer.parseInt(user)
将数字字符串转换为if语句中的int将会起作用,但它仍然给我错误消息。
任何帮助是极大的赞赏...
您可以尝试使用user.equals(“1”)而不是Integer.parseInt(user)== 1.或者假设异常java.lang.NumberFormatException有效并捕获该异常并以正确的方式对待它,例如:
System.out.println("where would you like to send the following item: ");
System.out.println("1. groceries, 2. savings, 3. utilities");
System.out.print("Enter list name or list number to send: ");
String user = user_input.nextLine();
try {
if (user.toLowerCase().equals("groceries") || Integer.parseInt(user) == 1) {
System.out.println("item was sent to the grocery list");
}else if(user.toLowerCase().equals("savings") || Integer.parseInt(user) == 2) {
System.out.println("item was sent to the savings list");
}else if(user.toLowerCase().contains("utilities") || Integer.parseInt(user) == 3) {
System.out.println("item was sent to the utilities list");
}else{
System.out.println("item is not in any category");
}
} catch (NumberFormatException nfe) {
System.out.println("item is not in any category");
}
更新:
System.out.println("where would you like to send the following item: ");
System.out.println("1. groceries, 2. savings, 3. utilities");
System.out.print("Enter list name or list number to send: ");
String user = user_input.nextLine();
if (user.toLowerCase().equals("groceries") || user.equals("1")) {
System.out.println("item was sent to the grocery list");
}else if(user.toLowerCase().equals("savings") || user.equals("2")) {
System.out.println("item was sent to the savings list");
}else if(user.toLowerCase().contains("utilities") || user.equals("3")) {
System.out.println("item was sent to the utilities list");
}else{
System.out.println("item is not in any category");
}
我不是100%肯定,但问题可能是当输入一个字符串时它仍然试图解析一个int,导致它失败。例如,输入“2”是有效的,因为“2”是一个有效的字符串,2是一个有效的int,可以从字符串“2”解析,而“groceries”是一个有效的字符串,但不包含一个有效的int,它可以被解析。
Odd822是对的。问题是当你输入“杂货”以外的任何字符串时,程序会尝试Integer.parseint(“stringwhichisnotaninteger”)。因此,它会引发异常。
这段代码应该有效:
if (user.toLowerCase().equals("groceries") || user.equals("1") ) {
System.out.println("item was sent to the grocery list");
}else if(user.toLowerCase().equals("savings") || user.equals("2")) {
System.out.println("item was sent to the savings list");
}else if(user.toLowerCase().contains("utilities") || user.equals("3")) {
System.out.println("item was sent to the utilities list");
}else{
System.out.println("item is not in any category");
}