使用Integer.parseInt获取NumberFormatException()

问题描述:

为什么我在这里获得NumberFormatException?我输入的代码值为1。无法理解为什么我得到这个例外使用Integer.parseInt获取NumberFormatException()

我用InputMismatchException因为早期我曾使用过扫描仪类的nextInt()方法,而不是Integer.parseInt()。还有刚才我已经采取int类型的,而不是字符串的代码,但现在修改它。

我认为它与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 

错误:

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:142) at 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) 
+0

现在不要扔了!也许它必须与在其他方法中使用的sc.nextLine()函数相关。 –

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); 
+0

没有它没有工作。它跳过代码的用户输入,然后再次抛出异常 –

+0

@SHACHINDRATRIPATHI你可以尝试将emtpy行放在顶部? –

+0

请参阅编辑 –

可能吧,它有一些空白。

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

+0

不幸的是:( –

+0

请参阅编辑 –

+0

检查没有空白字符串将要解析,可能是你之前得到异常 – 2017-09-14 11:14:30