-1

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);
}
Deadpool
  • 29,532
  • 9
  • 38
  • 73
  • You enter `S1` from a prompt using `nextInt()`, and you're confused that `S1` is rejected as a valid input for an `int` value? Javadoc of [`nextInt()`](https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextInt--) says: *"Throws `InputMismatchException` if the next token does not match the Integer regular expression, or is out of range"* – Andreas Jul 27 '18 at 18:21
  • 1
    It would be helpful to readers if you formatted your code a bit better. – 0xCursor Jul 27 '18 at 18:26
  • Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – LoolKovsky Jul 27 '18 at 18:27
  • Also please trim your code down to a [mcve] for the next question. – Robert Jul 27 '18 at 19:33

3 Answers3

1

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.

Check the Input you get!

Santosh b
  • 534
  • 4
  • 17
Akar
  • 376
  • 3
  • 6
0

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.

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.

num1 = Sc.nextInt();
       Sc.nextLine();

a.setQuantity(num1);

S1 = Sc.nextLine();

a.setName(S1);

num2 = Sc.nextInt();

a.setPrice(num2);
Deadpool
  • 29,532
  • 9
  • 38
  • 73
0

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.

foraintr
  • 436
  • 3
  • 8