Scanner.nextLine()引发java.util.InputMismatchException

时间:2018-07-27 18:13:04

标签: java class

我是新来的班级之类的东西。我现在要做的就是使库存容纳一个物料,然后在工作时,我将研究如何制作多个物料对象以及如何使程序继续运行,以便“编辑”和“删除”方法工作。因此,基本上,这是对库存和物品项目的测试。

库存类别:     当我运行项目并输入一个字符串:(S1)时,它告诉我:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at inventory.items.Inventory.add(Inventory.java:24)
at inventory.items.Inventory.main(Inventory.java:98)

请记住,我是一个初学者,您告诉我的任何事情都会有所帮助。我似乎无法弄清楚此代码是否正常工作。

public class Inventory extends Item implements Actions{
int num1, num2;
String S1;
Item a = new Item();
Item b = new Item();   
Scanner Sc = new Scanner(System.in);
@Override
public void add() {

System.out.println("Please enter the new item specifications as follows: 1. Quantity  2. Name  3. Price");

 num1 = Sc.nextInt();

 a.setQuantity(num1);

 S1 = Sc.nextLine();

 a.setName(S1);

 num2 = Sc.nextInt();

 a.setPrice(num2);

}

/**
 *
 * @param b
 */
@Override
public void remove(Item b) {
b = null;
System.gc();   
}

/**
 *
 * @param E
 */
@Override
public void edit(String E) {
E = Sc.nextLine();

if(a.getName().equals(b)){
System.out.println("Please edit the item specifications as follows: 1. Quantity  2. Name  3. Price ");

 num1 = Sc.nextInt();

 S1 = Sc.nextLine();

 num2 = Sc.nextInt();

 a.setQuantity(num1);

 a.setName(S1);

 a.setPrice(num2);   
 }
 else{
 System.out.println("You can't edit a non existent item. The items are case 
 sensitive.");    
 }
 }

 /**
 *
 * @param Info
 */
 @Override
 public void info(String Info) {
 if(a.getName().equals(Info)){    
 System.out.println("Item name : " + a.getName() + " Item price: " + 
 a.getPrice() + " Item Quantity: "  + a.getQuantity());
 }
 else{
 System.out.println("Please enter a an existing item name!");        
 }

 }
 public static void main (String [] args){
 Inventory inv = new Inventory();

 Scanner Sc1 = new Scanner(System.in);

 Item a = new Item();
 Item b = new Item();   

 String I = "";

 System.out.println("Please enter one of the following commands: 1. add   2. 
 edit   3. remove    4. info ");

  I = Sc1.nextLine();

 switch (I){
case "add" :
  inv.add();
  break;
case "edit" :
  System.out.println("Enter the name of the item you want to edit: ");
  String Input2 = Sc1.nextLine();
  inv.edit(Input2);
  break;
case "remove" :
  System.out.println("Enter the name of the item you want to remove: ");
  inv.remove(a);
  break;
case "info" :
  System.out.println("Enter the name of the item you want too see its info.");
  String Inf = Sc1.nextLine();
  inv.info(Inf);
default : 
  System.out.println("Please enter a valid command! The commands ARE case- 
  sensitive.");
}
}   
}

物品类别:

这是库存类的超类。我正在使用getter和setter在其中设置新对象的值。

class Item{

private int q,p;
private String n;

private int quantity =  0;
private String name = " ";
private int price = 0;

public void setQuantity(int q){
this.quantity = q;
}
public int getQuantity(){
return q;
}

public void setName(String n){
this.name = n;
}
public String getName(){
return n;
}
public void setPrice(int p){
this.price = p;
}
public int getPrice(){
return p;
}
}

动作界面:

我正在使用此界面设置可以对Item对象进行的操作;更改其属性。

interface Actions {
void add();
void remove(Item b);
void edit(String E);
void info(String Info);
}

3 个答案:

答案 0 :(得分:1)

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt()

  

InputMismatchException-如果下一个标记与Integer正则表达式不匹配或超出范围

您已经在一个字符串中输入了期望的整数,这就是引发此异常的原因。请务必先阅读JavaDosc。这是您的第一个指针,它将帮助您消除大多数“我不知道”的错误。

检查您得到的输入!

答案 1 :(得分:0)

可能Scanner.next()会解决您的问题,因为Scanner.nextInt()只会读取int值而不是行尾,而Scanner.nextLine()则会读取行尾并带有数字。

num1 = Sc.nextInt();

a.setQuantity(num1);

S1 = Sc.next();

a.setName(S1);

num2 = Sc.nextInt();

a.setPrice(num2);

只需添加Sc.nextLine()即可读取行上带有数字的其余部分。

num1 = Sc.nextInt();
       Sc.nextLine();

a.setQuantity(num1);

S1 = Sc.nextLine();

a.setName(S1);

num2 = Sc.nextInt();

a.setPrice(num2);

答案 2 :(得分:0)

获取num字段并输入后,下一行将由S1 = Sc.nextLine();读取。并且您正在尝试写入名称字段,但是num2 = Sc.nextInt();在跑。因此,您会遇到错误。