여기에 NumberFormatException
이 표시되는 이유는 무엇입니까? 내가 대신 내가 대신 문자열의 int 형의 코드를 촬영했다 이전 Integer.parseInt()
.Also, 나는이 예외Integer.parseInt()를 사용하여 NumberFormatException 받기
앞에서 내가 스캐너 클래스의 nextInt()
방법을 사용했기 때문에 InputMismatchException
을 사용있어 이유를 이해 할 수없는 1로 코드 값을 입력 그러나 지금 그것을 변경했다.
나는 이것이 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;
}
출력 :
012,346,230,239,362,414,
지금 던지지 말 것! 어쩌면 다른 메소드에서 사용 된 sc.nextLine() 함수와 관련이있을 수도 있습니다. –