首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Scanner.nextLine()抛出java.util.InputMismatchException

Scanner.nextLine()抛出java.util.InputMismatchException
EN

Stack Overflow用户
提问于 2018-07-28 02:13:04
回答 2查看 858关注 0票数 -1

我对课程和类似的东西都不熟悉。现在我所要做的就是让库存包含一个物品,然后当它工作时,我将研究如何创建多个物品对象,以及如何让程序继续运行,以便"edit“和"remove”方法工作。因此,这基本上是对清单和项目的测试。

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

代码语言:javascript
复制
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)

请记住,我是一个初学者,你告诉我的任何事情都会有所帮助。我似乎不知道如何让这段代码正常工作。

代码语言:javascript
复制
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.");
}
}   
}

Item类:

这是Inventory类的超类。我使用getter和setter来设置其中新对象的值。

代码语言:javascript
复制
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对象执行的操作;更改它的属性。

代码语言:javascript
复制
interface Actions {
void add();
void remove(Item b);
void edit(String E);
void info(String Info);
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-28 02:26:20

Scanner.next()可能会解决您的问题,因为Scanner.nextInt()将只读取int值,而不是行尾,而Scanner.nextLine()将读取包含数字的行的其余部分。

代码语言:javascript
复制
num1 = Sc.nextInt();

a.setQuantity(num1);

S1 = Sc.next();

a.setName(S1);

num2 = Sc.nextInt();

a.setPrice(num2);

您可以简单地添加Sc.nextLine()来读取行的其余部分及其上的数字。

代码语言:javascript
复制
num1 = Sc.nextInt();
       Sc.nextLine();

a.setQuantity(num1);

S1 = Sc.nextLine();

a.setName(S1);

num2 = Sc.nextInt();

a.setPrice(num2);
票数 0
EN

Stack Overflow用户

发布于 2018-07-28 02:27:15

在您获得num字段并输入它之后,下一行将由S1 = Sc.nextLine();读取,并且您正在尝试写入name字段,但num2 = Sc.nextInt();正在运行。由于这个原因,你得到了错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51563406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档