【问题标题】:Getting NumberFormatException using Integer.parseInt() [duplicate]使用 Integer.parseInt() 获取 NumberFormatException [重复]
【发布时间】:2018-02-23 07:39:47
【问题描述】:

为什么我在这里收到NumberFormatException?我输入的代码值为 1。无法理解为什么会出现此异常

我使用了InputMismatchException,因为之前我使用了Scanner 类的nextInt() 方法,而不是Integer.parseInt()。而且之前我使用了Int 类型的代码而不是String,但现在对其进行了修改。

我认为它与 sc.nextLine() 有关,但不使用它会在运行时跳过用户输入。

  public void searchItem() {

    String code = "";
    NewItem foundItem;
    String searchdisString = "";
    int finalCode = 0;

    if (ItemList != null && ItemList.size() > 0) {
        System.out.println("Enter Item code:");
        try {
            code = sc.nextLine();
            sc.nextLine();

            // Line 142 below
            finalCode = Integer.parseInt(code);
        } catch (InputMismatchException e) {
            System.out.println("Please enter a valid code.");
            return;
        }
        foundItem = search(code);
        if (foundItem == null) {
            System.out.println("Item not found");
            return;
        }

        else {
            System.out.println(foundItem.toString());
        }
    } else {
        System.out.println("No items to search. Please go to #3 to add items first.\nThank you.");
    }
}

输出:

  New Shop for Items created.
-----ITEM------
1. Display all items
2. Search items
3. Add items to list
4. Add items to cart
5. Display cart
6. Issue item
7. Exit
Choice:
3
Enter Item code:
1
Item name : 
apple
apple 
Rate : 
20
Quantity : 
30
1. Display all items
2. Search items
3. Add items to list
4. Add items to cart
5. Display cart
6. Issue item
7. Exit
Choice:
2
Enter Item code:
1

错误:

线程“main”java.lang.NumberFormatException 中的异常:对于输入 字符串:“”在
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在 java.lang.Integer.parseInt(Integer.java:504) 在 java.lang.Integer.parseInt(Integer.java:527) 在 NewShop.searchItem(NewShop.java:142) 在 NewShoppingCart.main(NewShoppingCart.java:45)

编辑:

if(ItemList!=null&&ItemList.size()>0)
      {
        System.out.println("Enter Item code:");
        try{
            code = sc.nextLine();

            finalCode = Integer.parseInt(code.trim());
           }
        catch(InputMismatchException e){
         System.out.println("Please enter a valid code.");
         return;
        }

输出:

New Shop for Items created.
-----ITEM------
1. Display all items
2. Search items
3. Add items to list
4. Add items to cart
5. Display cart
6. Issue item
7. Exit
Choice:
3   
Enter Item code:
1
Item name : 
APPLE
20
APPLE 
Rate : 
30
Quantity : 
20
1. Display all items
2. Search items
3. Add items to list
4. Add items to cart
5. Display cart
6. Issue item
7. Exit
Choice:
2
Enter Item code:
Exception in thread "main" java.lang.NumberFormatException: For input   string: ""
 at    java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at NewShop.searchItem(NewShop.java:141)
at NewShoppingCart.main(NewShoppingCart.java:45)

【问题讨论】:

  • 现在不扔了!也许它与其他方法中使用的 sc.nextLine() 函数有关。

标签: java exception numberformatexception parseint


【解决方案1】:
code = sc.nextLine();
sc.nextLine(); // reading extra line

// Line 142 below
finalCode = Integer.parseInt(code);

您正在阅读一个空的额外行。只需将其删除即可。

sc.nextLine(); // reading extra line
code = sc.nextLine();
// Line 142 below
finalCode = Integer.parseInt(code);

【讨论】:

  • 不,它不起作用。它跳过了用户输入的代码,然后再次抛出异常
  • @SHACHINDRATRIPATHI 你能试着把空行放在上面吗?
  • 请参阅编辑
【解决方案2】:

可能是,它有一些空白。

试试这个:finalCode = Integer.parseInt(code.trim());

【讨论】:

  • 很遗憾没有:(
  • 请参阅编辑
  • 检查一下没有空白字符串会被解析,可能是你在此之前遇到了异常
猜你喜欢
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 2017-01-26
  • 1970-01-01
  • 2021-04-17
  • 1970-01-01
相关资源
最近更新 更多