0

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)
 
 
Filburt
  • 17,626
  • 12
  • 64
  • 115
  • `name == "" ` is not how to compare Strings in java – Scary Wombat Oct 27 '21 at 06:39
  • `locationCode = input.nextLine(); locationCode = input.nextLine();` - why twice? – Scary Wombat Oct 27 '21 at 06:42
  • `boolean fragility = input.hasNextFloat();` here, your input checks _if there is a float_. There is not, since you entered "false". So the input in not consumed. The next call is `quantity = input.nextInt()`, so you try to read an `int`, but the scanner reads "false", hence the exception. – maloomeister Oct 27 '21 at 06:43
  • when i run it once it does not ask user to input the first and skips to the second one directly. so when i put twice , it works and user to input value – Shangkari Sha Oct 27 '21 at 06:44
  • @ScaryWombat probably due to https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo – maloomeister Oct 27 '21 at 06:45
  • if so, how shall i ask user to input boolean and avoid the exception in the quantity? – Shangkari Sha Oct 27 '21 at 06:45
  • @maloomeister - I thought it was this, but as the OP assigned to a variable I thought it might be a typo. OP usually just call `scn.nextLine();` without assigning to a variable and add a comment. – Scary Wombat Oct 27 '21 at 06:49
  • hi maloomeister, i just saw the link you had send to stop printing the user input twice. but it my case, how shall i do it? – Shangkari Sha Oct 27 '21 at 06:49
  • 1
    Use [`nextBoolean()`](https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextBoolean--) to read a boolean? – maloomeister Oct 27 '21 at 06:50
  • @maloomeister. thank you for the help to solve the error. it take lots of time to solve that. may i know an example to solve typing the printout twice in my code in case? – Shangkari Sha Oct 27 '21 at 06:58

0 Answers0