In my code, the problem runs using a switch case and asking for user inputs.
All the user input works but when it comes to the line "Insert product quantity:" it shows the following error
"Exception in thread "main" java.util.InputMismatchException".
However, I had input the correct format which is int. below is the code and the example. I am running apache netbeans.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class LSystem
{
private static Warehouse[] warehouses = new Warehouse[0];
private static Site[] sites = new Site[0];
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int option, quantity, index, fromIndex, toIndex;
String name, site, locationCode, choice, arrival;
while(true)
{
System.out.println("Welcome to MyDelivery Information System ");
System.out.println("**************************************************");
System.out.println("Enter: ");
System.out.println("1. Create warehouse ");
System.out.println("2. Create site ");
System.out.println("3. Insert product ");
System.out.println("4. Move product from warehouse to site ");
System.out.println("5. Move product from site to site");
System.out.println("6. Exit program ");
System.out.println("Enter your option(in number) here: ");
option = input.nextInt();
switch (option)
{
case 1:
System.out.println("\n\n");
System.out.println("**************************************************");
System.out.println("You had enter Option1: Create warehouse \n");
System.out.println("Insert warehouse locationCode: ");
locationCode = input.nextLine();
locationCode = input.nextLine();
System.out.println("Insert warehouse name: ");
name = input.nextLine();
if (name == "" || locationCode == "")
{
System.out.println("Invalid Input");
break;
}
createWarehouse(name, locationCode);
break;
case 2:
System.out.println("\n\n");
System.out.println("**************************************************");
System.out.println("You had enter Option2: Create site");
System.out.println("Insert site name: ");
name = input.nextLine();
name = input.nextLine();
System.out.println("Insert warehouse locationCode: ");
locationCode = input.nextLine();
createSite(name, locationCode);
break;
case 3:
System.out.println("\n\n");
System.out.println("**************************************************");
System.out.println("You had enter Option3: Insert product \n");
for (int i = 0; i < warehouses.length; i++) {
System.out.println("index " + i + ": " + warehouses[i].name);
}
System.out.println("\n Enter warehouseIndex name to continue: ");
index = input.nextInt();
if (index < warehouses.length)
{
System.out.println("Insert product name: ");
name = input.nextLine();
name = input.nextLine();
System.out.println("Insert product fragility(true or false): ");
boolean fragility = input.hasNextFloat();
System.out.println("Insert product quantity: "); //SHOWS ERROR IN HERE ONLY
quantity = input.nextInt();
quantity = input.nextInt();
insertProduct(name, fragility, quantity, index);
}
else
{
System.out.println("Invalid warehouseIndex");
}
break;
case 4:
System.out.println("\n\n");
System.out.println("**************************************************");
System.out.println("You had enter Option4: Move product from warehouse to sites \n");
for (int i = 0; i < warehouses.length; i++) {
System.out.println("index " + i + ": " + warehouses[i].name);
}
System.out.println("\nEnter warehouse index to continue: ");
fromIndex = input.nextInt();
for (int i = 0; i < sites.length; i++) {
System.out.println("index " + i + ": " + sites[i].name);
}
System.out.println("\nEnter site index to continue: ");
toIndex = input.nextInt();
System.out.println("Insert product name: ");
name = input.nextLine();
name = input.nextLine();
System.out.println("Insert product quantity");
quantity = input.nextInt();
//quantity = input.nextInt();
System.out.println("Insert product arrival date");
arrival = input.nextLine();
arrival = input.nextLine();
moveProductWarehouseToSite(name, quantity, arrival, fromIndex, toIndex);
break;
case 5:
System.out.println("\n\n");
System.out.println("**************************************************");
System.out.println("You had enter Option5: Move product from site to sites \n");
for (int i = 0; i < sites.length; i++) {
System.out.println("index " + i + ": " + sites[i].name);
}
System.out.println("\nEnter from site index to continue: ");
fromIndex = input.nextInt();
System.out.println("\nEnter to site index to continue: ");
toIndex = input.nextInt();
System.out.println("Insert product name: ");
name = input.nextLine();
name = input.nextLine();
System.out.println("Insert product quantity");
quantity = input.nextInt();
//quantity = input.nextInt();
System.out.println("Insert product arrival date");
arrival = input.nextLine();
arrival = input.nextLine();
moveProductSiteToSite( name, quantity, arrival, fromIndex, toIndex);
break;
default:
System.out.println("Invalid Input. Please try again later.");
break;
}
if (option == 6)
{
break;
}
}
}
// function for case 1
public static boolean createWarehouse(String name, String locationCode)
{
Warehouse newWarehouse = new Warehouse(name, locationCode);
ArrayList<Warehouse> newWarehouses = new ArrayList<Warehouse>(Arrays.asList(warehouses));
newWarehouses.add(newWarehouse);
warehouses = newWarehouses.toArray(warehouses);
return true;
}
// function for case 2
public static boolean createSite(String name, String locationCode)
{
Site newSite = new Site(name, locationCode);
ArrayList<Site> newSites = new ArrayList<Site>(Arrays.asList(sites));
newSites.add(newSite);
sites = newSites.toArray(sites);
return true;
}
// function for case 3
public static boolean insertProduct(String name, boolean fragile, int quantity, int warehouseIndex)
{
Product newProduct = new Product(quantity, fragile, name);
warehouses[warehouseIndex].insertProduct(newProduct);
return true;
}
// function for case 4
public static boolean moveProductWarehouseToSite(String name, int quantity, String arrival, int fromIndex, int toIndex)
{
// create new instruction
Product newInstruction = new Product(quantity, false, name);
newInstruction.setArrival(arrival);
newInstruction.setFrom(warehouses[fromIndex]);
// remove from warehouse
newInstruction.setQuantity(-quantity);
warehouses[fromIndex].moveProduct(newInstruction);
// move to site
newInstruction.setQuantity(quantity);
sites[toIndex].moveProduct(newInstruction);
return true;
}
// function for case 5
public static boolean moveProductSiteToSite(String name, int quantity, String arrival, int fromIndex, int toIndex)
{
// create new instruction
Product newInstruction = new Product(quantity, false, name);
newInstruction.setArrival(arrival);
newInstruction.setFrom(sites[fromIndex]);
// remove from warehouse
newInstruction.setQuantity(-quantity);
sites[fromIndex].moveProduct(newInstruction);
// move to site
newInstruction.setQuantity(quantity);
sites[toIndex].moveProduct(newInstruction);
return true;
}
}
the error is show in this statement in case 3,4 and 5:
System.out.println("Insert product quantity");
quantity = input.nextInt();
//quantity = input.nextInt();
and here is the output example:
Welcome to MyDelivery Information System
**************************************************
Enter:
1. Create warehouse
2. Create site
3. Insert product
4. Move product from warehouse to site
5. Move product from site to site
6. Exit program
Enter your option(in number) here:
1
**************************************************
You had enter Option1: Create warehouse
Insert warehouse locationCode:
43200
Insert warehouse name:
btho
Welcome to MyDelivery Information System
**************************************************
Enter:
1. Create warehouse
2. Create site
3. Insert product
4. Move product from warehouse to site
5. Move product from site to site
6. Exit program
Enter your option(in number) here:
2
**************************************************
You had enter Option2: Create site
Insert site name:
suasana
Insert warehouse locationCode:
10
Welcome to MyDelivery Information System
**************************************************
Enter:
1. Create warehouse
2. Create site
3. Insert product
4. Move product from warehouse to site
5. Move product from site to site
6. Exit program
Enter your option(in number) here:
3
**************************************************
You had enter Option3: Insert product
index 0: btho
Enter warehouseIndex name to continue:
0
Insert product name:
shirt
Insert product fragility(true or false):
false
Insert product quantity:
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at Assignment2.LSystem.main(LSystem.java:92)