Scanner.nextLine()抛出java.util.InputMismatchException [英] Scanner.nextLine() throws java.util.InputMismatchException

查看:84
本文介绍了Scanner.nextLine()抛出java.util.InputMismatchException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to classes and stuff like that. All I'm trying to do for now is to make the inventory hold one item and then when it works I'll research on how to make multiple Item objects and how to keep the program going so the "edit" and "remove" methods work. So basically this a test for an inventory and items project.

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

Inventory class: When I run the project and enter a String: (S1) it tells me this:

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)

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

Keep in mind that I'm a beginner and any thing you'll tell me will help. I can't seem to figure out to get this code working.

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来设置新对象的值。

This is the superclass of the Inventory class. I'm using getters and setters to set values of new objects in it.

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对象进行的操作;

I'm using this interface to set the actions that can be made to an Item object; changing it's properties.

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


推荐答案

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

Probably Scanner.next() will solve your problem because Scanner.nextInt() will just read the int value and not the end of line, while Scanner.nextLine() reads the remainder of the line with the number on it.

num1 = Sc.nextInt();

a.setQuantity(num1);

S1 = Sc.next();

a.setName(S1);

num2 = Sc.nextInt();

a.setPrice(num2);

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

simply you can add Sc.nextLine() to read the remainder of the line with the number on it.

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

a.setQuantity(num1);

S1 = Sc.nextLine();

a.setName(S1);

num2 = Sc.nextInt();

a.setPrice(num2);

这篇关于Scanner.nextLine()抛出java.util.InputMismatchException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆