0

this part of code should load name of restoraunt, then menu name, then meal name and price but when i run it, it takes in all the names and then when it finally comes to price, i input for example 7.2 and get input mismatch exception

  String newMenu = "";
  String newRestaurant = "";
  String[] newMenuItem = new String[10];
  double[] price = new double[10];
  int x = 0;  


       while (!(newMenu.equals("none"))) {
           System.out.println("What is the name of the Menu you wish to create (type 'none', if you are done):");
           newMenu = scan.next();
           if (newMenu.equals("none")) {
              System.out.println("Saving entry...");
              continue;

           } else {


              System.out.println("What is the name of the Menu item you wish to create (type 'none', if you are done):");
              newMenuItem[x] = "end";
              while (!(newMenuItem[x].equals("none"))) {
                 newMenuItem[x] = scan.next();

                 if (!(newMenuItem[x].equals("none"))) {
                    System.out.println("What is the price?");
                    price[x]= scan.nextDouble();
                    x++;


                    }    
                 }    
              }
           }
Jørgen R
  • 10,568
  • 7
  • 42
  • 59
user2627736
  • 281
  • 1
  • 4
  • 13
  • yes, but this time the code is much cleaner, i fixed alot of things, this is the last thing, last part of the puzzle, if inputing numbers works i'm complete with my assignment which is awsome bcs it's 4 am and i rly need some sleep, need to hand this in before tomorrow morning – user2627736 Jun 09 '14 at 01:16
  • @user2627736 post the error.. – Rod_Algonquin Jun 09 '14 at 01:16
  • Exception in thread "main" java.util.InputMismatchException at java.util. Scanner.throwFor(Scanner.java:909) at java.util. Scanner.next(Scanner.java:1530) at java.util. Scanner.nextDouble(Scanner.java:2456) at RestaurantTest2.main(RestaurantTest2.java:80) Btw, line 80 is "price[x]= scan.nextDouble();" – user2627736 Jun 09 '14 at 01:18
  • @user2627736 its just your input, maybe you add a character or space – Rod_Algonquin Jun 09 '14 at 01:20
  • Is it maybe leftover from previous scans? Because they were strings. How would I clean it if it's leftover. I only type in 7.2 and press enter when it asks for price – user2627736 Jun 09 '14 at 01:23
  • @user2627736 I tried it and it works.. – Rod_Algonquin Jun 09 '14 at 01:27
  • Next time, please update your original Question. – Stephen C Jun 09 '14 at 01:32
  • Tried what? Running entire program? It didn't give any exceptions in it's current state without any modifications? – user2627736 Jun 09 '14 at 01:33

1 Answers1

0

If I understand your program's input, the simplest solution would be to add something like this between lines 79 and 80,

while (!scan.hasNextDouble()) {
  // The next token isn't a double.
  if (scan.hasNext()) {
    scan.next();
  } else { 
    System.err.println("Nothing to read");
    System.exit(1);
  }
}

Because, currently your Scanner doesn't have the next symbol as a double.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249