我不是java的新手,但我无法弄清楚为什么会出现这个Scanner错误。代码编译得很好,但我得到以下运行时错误
输入项目编号:线程“main”中的异常java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:840) 在java.util.Scanner.next(Scanner.java:1461) 在java.util.Scanner.nextInt(Scanner.java:2091) 在java.util.Scanner.nextInt(Scanner.java:2050) 在TestInvoice.getValues(TestInvoice.java:3
import java.util.Scanner;
public class TestInvoice {
public static void main(String [] args)
{
/* create objects */
Invoice item1 = new Invoice();
Invoice item2 = new Invoice();
Invoice item3 = new Invoice();
/* get values */
getValues(item1);
getValues(item2);
getValues(item3);
/* print objects */
System.out.println(item1.toString() + "\n");
System.out.println(item2.toString() + "\n");
System.out.println(item3.toString() + "\n");
}
private static void getValues(Invoice invoice)
{
int number;
String name;
int quantity;
double price;
/* get values from user */
Scanner scanner = new Scanner("System.in");
System.out.print("Enter item number: ");
number = scanner.nextInt();
System.out.print("Enter item name: ");
name = scanner.next();
System.out.print("Enter quantity: ");
quantity = scanner.nextInt();
System.out.print("Enter item price: ");
price = scanner.nextDouble();
System.out.println();
/* set the values */
invoice.setItemNumber(number);
invoice.setItemName(name);
invoice.setItemQuantity(quantity);
invoice.setItemPrice(price);
}
}
答案 0 :(得分:11)
也许改变
Scanner scanner = new Scanner("System.in");
到
Scanner scanner = new Scanner(System.in);