Inventory and item program (Keep getting error when I try to enter String input)
Inventory and item program (Keep getting error when I try to enter String input)
I'm new to classes and stuff like that. All I'm trying to do for now is to make the inventory hold one item and then when it works I'll research on how to make multiple Item objects and how to keep the program going so the "edit" and "remove" methods work. So basically this a test for an inventory and items project.
Inventory class:
When I run the project and enter a String: (S1) it tells me this:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at inventory.items.Inventory.add(Inventory.java:24)
at inventory.items.Inventory.main(Inventory.java:98)
Keep in mind that I'm a beginner and any thing you'll tell me will help. I can't seem to figure out to get this code working.
public class Inventory extends Item implements Actions{
int num1, num2;
String S1;
Item a = new Item();
Item b = new Item();
Scanner Sc = new Scanner(System.in);
@Override
public void add() {
System.out.println("Please enter the new item specifications as follows: 1. Quantity 2. Name 3. Price");
num1 = Sc.nextInt();
a.setQuantity(num1);
S1 = Sc.nextLine();
a.setName(S1);
num2 = Sc.nextInt();
a.setPrice(num2);
}
/**
*
* @param b
*/
@Override
public void remove(Item b) {
b = null;
System.gc();
}
/**
*
* @param E
*/
@Override
public void edit(String E) {
E = Sc.nextLine();
if(a.getName().equals(b)){
System.out.println("Please edit the item specifications as follows: 1. Quantity 2. Name 3. Price ");
num1 = Sc.nextInt();
S1 = Sc.nextLine();
num2 = Sc.nextInt();
a.setQuantity(num1);
a.setName(S1);
a.setPrice(num2);
}
else{
System.out.println("You can't edit a non existent item. The items are case
sensitive.");
}
}
/**
*
* @param Info
*/
@Override
public void info(String Info) {
if(a.getName().equals(Info)){
System.out.println("Item name : " + a.getName() + " Item price: " +
a.getPrice() + " Item Quantity: " + a.getQuantity());
}
else{
System.out.println("Please enter a an existing item name!");
}
}
public static void main (String args){
Inventory inv = new Inventory();
Scanner Sc1 = new Scanner(System.in);
Item a = new Item();
Item b = new Item();
String I = "";
System.out.println("Please enter one of the following commands: 1. add 2.
edit 3. remove 4. info ");
I = Sc1.nextLine();
switch (I){
case "add" :
inv.add();
break;
case "edit" :
System.out.println("Enter the name of the item you want to edit: ");
String Input2 = Sc1.nextLine();
inv.edit(Input2);
break;
case "remove" :
System.out.println("Enter the name of the item you want to remove: ");
inv.remove(a);
break;
case "info" :
System.out.println("Enter the name of the item you want too see its info.");
String Inf = Sc1.nextLine();
inv.info(Inf);
default :
System.out.println("Please enter a valid command! The commands ARE case-
sensitive.");
}
}
}
Item class:
This is the superclass of the Inventory class. I'm using getters and setters to set values of new objects in it.
class Item{
private int q,p;
private String n;
private int quantity = 0;
private String name = " ";
private int price = 0;
public void setQuantity(int q){
this.quantity = q;
}
public int getQuantity(){
return q;
}
public void setName(String n){
this.name = n;
}
public String getName(){
return n;
}
public void setPrice(int p){
this.price = p;
}
public int getPrice(){
return p;
}
}
Actions interface:
I'm using this interface to set the actions that can be made to an Item object; changing it's properties.
interface Actions {
void add();
void remove(Item b);
void edit(String E);
void info(String Info);
}
S1
nextInt()
S1
int
nextInt()
InputMismatchException
It would be helpful to readers if you formatted your code a bit better.
– LAD
8 mins ago
Possible duplicate of Scanner is skipping nextLine() after using next() or nextFoo()?
– Toth Ludovic Andreas
7 mins ago
3 Answers
3
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt():
InputMismatchException - if the next token does not match the Integer regular expression, or is out of range
You have entered a string where it expected an integer, thats why this exception is thrown. Always read the JavaDosc first. That's your first pointer, that will help you eliminate the most of the "i don't know"-errors.
Never trust the input you get, always check if it is in your expected boundary. Then convert it or show up an error.
After you get num field and enter it, next line will be readed by S1 = Sc.nextLine(); and you are trying to write name field, but num2 = Sc.nextInt(); is running. for this reason you are getting error.
Probably Scanner.next()
will solve your problem because Scanner.nextInt()
will just read the int value and not the end of line, while Scanner.nextLine()
reads the remainder of the line with the number on it.
Scanner.next()
Scanner.nextInt()
Scanner.nextLine()
num1 = Sc.nextInt();
a.setQuantity(num1);
S1 = Sc.next();
a.setName(S1);
num2 = Sc.nextInt();
a.setPrice(num2);
or
simply you can add Sc.nextLine()
to read the remainder of the line with the number on it.
Sc.nextLine()
num1 = Sc.nextInt();
Sc.nextLine();
a.setQuantity(num1);
S1 = Sc.nextLine();
a.setName(S1);
num2 = Sc.nextInt();
a.setPrice(num2);
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You enter
S1
from a prompt usingnextInt()
, and you're confused thatS1
is rejected as a valid input for anint
value? Javadoc ofnextInt()
says: "ThrowsInputMismatchException
if the next token does not match the Integer regular expression, or is out of range"– Andreas
13 mins ago