2016-09-21 62 views
0

我目前正在使用java在程序中苦苦挣扎。该计划目前情况良好。但是,我犯了一个小错误。对于案例1(添加项目),我打算在制造商中用空格键入字符串。发生在Eclipse中的错误这样通过在空间键入Java。 InputMismatchException

Exception in thread "main" java.util.InputMismatchException 
at java.util.Scanner.throwFor(Unknown Source) 
at java.util.Scanner.next(Unknown Source) 
at java.util.Scanner.nextDouble(Unknown Source) 
at ass1.inventory.addItem(inventory.java:44) 
at ass1.inventory.run(inventory.java:28) 
at ass1.inventory.main(inventory.java:11) 

我做了很多的研究对这个问题,比如增加使用in.nextLine()代替in.next(),错误仍然走了出来。有没有人可以为我指出问题并解决问题?非常感谢你!!

这是我的代码:

import java.util.Scanner; 

public class inventory { 

private static item[] inventory; 
static Scanner scanner = new Scanner(System.in); 
private static int noOfItems; 

public static void main(String[] args) { 
    noOfItems=0;   
    // TODO Auto-generated method stub 
    inventory=new item[5]; 
    run();  
} 
public static int displayMenu() 
{ 
    System.out.println("1.Add a product"); 
    System.out.println("2.Display a product"); 
    System.out.println("3.Quit"); 
    int i=scanner.nextInt(); 
    return i; 
} 
public static void run() 
{ 
    while(true) 
    { 
     int i=displayMenu(); 
     switch(i) 
     { 
     case 1:addItem(); 
       break; 
     case 2:findItem(); 
       break; 
     case 3:return; 
     default:System.out.println("Invalid choice"); 
     } 
    } 
} 
public static void addItem() 
{ 
    System.out.print("Enter Item name:"); 
    String item_name=scanner.next(); 
    System.out.print("Enter the manufacturer:"); 
    String manufacturer=scanner.next(); 
    System.out.print("Enter price:"); 
    double price=scanner.nextDouble(); 

    item b=new item(item_name,manufacturer,price); 
    if(noOfItems==inventory.length) 
     System.out.println("Array is full"); 
    else 
    { 
     inventory[noOfItems++]=b; 
     System.out.println("Item added successfully"); 
    } 
} 
public static void findItem() 
{ 
    System.out.print("Enter item name:"); 
    String item_name=scanner.next(); 
    for(int i=0; i<noOfItems; i++) 
    { 
     if(item_name(inventory[i].getItem_name())) 
     { 
      System.out.println("Item found:"); 
      System.out.print(inventory[i]+"\n"); 
      return; 
     }       
    }  
} 
} 

这是另一个文件连接到第一个:

public class item { 

    private String item_name; 
    private String manufacturer; 
    private double price; 

    //To initialise the state of the object 
    public item(String item_name,String manufacturer,double price) 
    { 
    this.item_name=item_name; 
    this.manufacturer=manufacturer; 
    this.price=price; 
    } 
    //Reader methods i.e behavior methods 
    public String getItem_name() 
    { 
    return item_name; 
    } 
    public String getManufacturer() 
    { 
    return manufacturer; 
    } 
    public double getPrice() 
    { 
    return price; 
    } 
    //Writer methods or setter methods public void setTitle(String item_name) 
    { 
    this.item_name=item_name; 
    } 
    public void setManufact(String manufacturer) 
    { 
    this.manufacturer=manufacturer; 
    } 
    public void setPrice(double price) 
    { 
    if(price < 0) 
     System.out.println("Price cannot be negative"); 
    else 
     this.price=price; 
    } 
    public String toString() 
    { 
    return "Item name:"+item_name+"\nManufacturer:"+manufacturer+"\nPrice:"+price; 
    } 
} 

回答

0

您可以在许多方面

解决问题

尝试在开头添加scanner.useDelimiter(System.getProperty("line.separator"));additem方法。

0

由于scanner.next()只能在空格前的下一个标记中读取,所以发生此错误。

现在,如果我要运行你的代码,修改一下显示值。如下所示。

System.out.print("Enter Item name:"); 
String item_name = scanner.next(); 
System.out.print("Enter the manufacturer:"); 
String manufacturer = scanner.next(); 
System.out.println("Manufacturer:" + manufacturer); //Print Manufacturer 
System.out.print("Enter price:"); 
double price = scanner.nextDouble(); 

如果我是写test test为制造商输入,只test将被分配变量制造商。将剩余的字符串留在缓冲区中。就像下面的结果一样。

Enter Item name:test 
Enter the manufacturer:test test 
Manufacturer:test 
Enter price:Exception in thread "main" java.util.InputMismatchException 
    at java.util.Scanner.throwFor(Unknown Source) 
    at java.util.Scanner.next(Unknown Source) 
    at java.util.Scanner.nextDouble(Unknown Source) 
    at stackOverflowQns.inventory.addItem(inventory.java:50) 
    at stackOverflowQns.inventory.run(inventory.java:32) 
    at stackOverflowQns.inventory.main(inventory.java:15) 

您提到使用scanner.nextLine()不起作用。这是为什么。

由于您已经使用了诸如scanner.next()scanner.nextInt()等的方法,所有这些方法都会将行分隔符(\n)留在缓冲区中。因此,当您随后调用scanner.nextLine()时,它将拾取行分隔符之前的任何内容并返回空字符串。这就是你看到这样的事情的地方。

Enter Item name:test 
Enter the manufacturer:Enter price: 

因此解决您的问题, 你必须从用户读取值之前,要么做一个scanner.nextLine(),只是为了清除行分隔符。如下所示。但这只是丑陋的,我不推荐。

System.out.print("Enter the manufacturer:"); 
scanner.nextLine(); 
String manufacturer = scanner.nextLine(); 

或做所提出什么其他的答案,这是设置为仅使用行分隔符作为分隔符,因此,解决所有提到的问题。