使用Integer.parseInt()获取NumberFormatException

时间:2017-09-14 10:49:18

标签: java exception numberformatexception parseint

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

我使用了InputMismatchException,因为之前我使用的是nextInt()方法的Scanner类,而不是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:用于输入   string:""在
  java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)   在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)

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

可能,它有一些空白。

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