Pourquoi je reçois NumberFormatException
ici? Je suis entré dans la valeur de code comme 1. Impossible de comprendre pourquoi je suis arrivé cette exceptionObtenir NumberFormatException en utilisant Integer.parseInt()
je InputMismatchException
parce que plus tôt j'avais utilisé la méthode nextInt()
de classe Scanner, au lieu de Integer.parseInt()
.Aussi plus tôt que j'avais pris code de type Int au lieu de chaîne, mais maintenant modifié.
Je pense que cela a à voir avec sc.nextLine()
, mais ne pas l'utiliser ignore l'entrée de l'utilisateur pendant l'exécution.
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.");
}
}
sortie:
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
Erreur:
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)
EDIT:
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;
}
sortie:
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)
Ne pas lancer maintenant! Peut-être qu'il devait faire avec la fonction sc.nextLine() utilisée dans une autre méthode. –