java - Scanner.nextLine() 抛出 java.util.InputMismatchException
问题描述
我是新来的类和类似的东西。我现在要做的就是让库存保持一件物品,然后当它工作时,我将研究如何制作多个物品对象以及如何保持程序运行,以便“编辑”和“删除”方法工作。所以基本上这是对库存和物品项目的测试。
Inventory 类:当我运行项目并输入 String: (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.");
}
}
}
物品类别:
这是 Inventory 类的超类。我正在使用 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);
}
解决方案
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt():
InputMismatchException - 如果下一个标记与 Integer 正则表达式不匹配,或者超出范围
您输入了一个字符串,它需要一个整数,这就是引发此异常的原因。始终首先阅读 JavaDosc。这是您的第一个指针,它将帮助您消除大部分“我不知道”的错误。
检查你得到的输入!
推荐阅读
- oracle - 如何使用 oracle 10g 服务器连接 Oracle 18c 服务器
- paypal - 如何将 PayPal 部分退款的正文添加到 Axios?
- java - 定义spring bean销毁方法顺序
- swiftui - SwiftUI ObservedObject 未更新
- c++ - 使用 CMake 和 C++ 时,我的测试从未停止。如何在不启动其主要功能的情况下测试程序?
- docker - Docker Swarm 中领导管理器节点上的服务放置
- amazon-redshift - 将当前日期添加到 Amazon Redshift 中的具体化视图
- javascript - 如何使用javascript将检索到的firebase数据设置为div
- django - 新 DRF 列表视图中的“无法将关键字‘创建’解析为字段”
- php - 使用远程服务器在 PHP 中存储和提供图像不起作用