我不是新來的java,但我不明白爲什麼我得到這個掃描儀錯誤。代碼編譯好,但我得到以下運行時錯誤Java掃描器 - 爲什麼我會得到這個錯誤?
輸入項目編號:異常線程「main」 java.util.InputMismatchException 在java.util.Scanner.throwFor(Scanner.java:840) 是java .util.Scanner.next(Scanner.java:1461) at java.util.Scanner.nextInt(Scanner.java:2091) at java.util.Scanner.nextInt(Scanner.java:2050) at 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);
}
}
刪除周圍'System.in'引號。 – 2010-10-10 21:20:54