-1

Could someone show me what i'm doing wrong, this part of code should scan for name of restaurant, menu name and names of menu items and their prices, but it's giving me some sort of exception, also it seems to skip completely the first scan, just jumps to second, ideally it should scan for 1 resturant that has 1 menu and that contains 2 menu items with their names and prices

String newRestaurant, newMenu;

String[] newMenuItem = { "", "" };

double[] price = {0.0, 0.0};

int x = 0; 

            System.out.println("---------------------------------------");
            System.out.println("CREATE A RESTAURANT, MENUS, and MENU ITEMS:");
            System.out.println("Please input the name of the new Restaurant:");
            newRestaurant = scan.nextLine();
            System.out.println("What is the name of the Menu you wish to create (type 'none', if you are done):");
            newMenu = scan.nextLine();
            if (newMenu == "none") System.out.println("Saving entry...");
            else {
               System.out.println("What is the name of the Menu item you wish to create (type 'none', if you are done):");
               newMenuItem[x] = scan.nextLine();
               if(newMenu != "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
  • 5
    [Don't compare `String`s with `==`. Use `.equals()` instead](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – Christian Tapia Jun 09 '14 at 00:05
  • 2
    How do you declare "scan"? Also, what exception happens? – BlueMoon93 Jun 09 '14 at 00:06
  • Be sure you are not calling `nextYYY()` (or similar) before the `nextLine()` without taking care of the problems it may lead to. More information [here](http://christprogramming.wordpress.com/2014/01/30/java-common-mistakes-1/) or [here](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx). – Christian Tapia Jun 09 '14 at 00:09
  • what is the exact exception ? – exexzian Jun 09 '14 at 00:09
  • exception is: 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:78) – user2627736 Jun 09 '14 at 00:12
  • Also, i changed == to .equals(), that part is okay now but the exceptions are still there – user2627736 Jun 09 '14 at 00:14
  • And the line # **78** in **RestaurantTest2.java** file is ... ? – PM 77-1 Jun 09 '14 at 00:14
  • Line 78 is a simple system.out.println – user2627736 Jun 09 '14 at 00:20
  • A simple `println` CANNOT throw `InputMismatchException`. Tell us what the real exception line is. – Stephen C Jun 09 '14 at 00:55

1 Answers1

0

First lets get the problem of your string testing out of the way:

Don't == or != to test strings in Java. Unless you really, really know what you are doing, == and != are liable to give your the wrong answer. Use s.equals(s2) or !s.equals(s2).

Your original code uses both == and != to compare strings, and this is a situation where the test will be unreliable. Fix all of these ...

Reference:


Now we have the problem of what is causing InputMismatchException to be thrown. You have told us that it is coming from a simple println, but that is virtually impossible. In reality, the stacktrace says that exception is being thrown by a nextDouble() call, and that will be happening because (as the javadoc says) "... the next token does not match the Float regular expression, or is out of range".

What is the cause? Well it could be two things:

  • It could be that the user has entered something that is not a number in the right syntax. For example, "four" or "my brain hurts".

    A properly written program should deal with this ... by telling the user he/she has made a mistake. (For example, use the hasDouble() method to test if the next token is acceptable as a double.)

  • It could be that a logic error in your code means that you are attempting to read the answer to some other question (or something) as a number.

I can't tell you which of those is the real cause, because frankly I don't trust the "evidence" that you have provided. I doubt that that is the real code, and it certainly doesn't match the stacktrace. Furthermore, you haven't told us what the "user" is typing to cause this to happen.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216