0

I made a program that allows the user to make a cheeseburger and then asks if they want a side item and then asks if they want to order another cheeseburger. When I run the program it skips over the last if else statement.

   String orderAnother = sc.nextLine();
if (orderAnother.equalsIgnoreCase("yes")) {
    System.out.println("Great! Another cheeseburger is on its way!");
} else {
    System.out.println("Okay! Thanks for stopping at The Burger Shack and have a great day!");
}

Is there something wrong that I can't figure out? Help is appreciated!

carlie
  • 1
  • 2
  • 2
    It would be nice if you included all of your code. Who knows, you could have a `System.exit(0)` somewhere in your code. – Jonny Henly Mar 28 '16 at 23:44
  • 1
    There is nothing wrong with this code. You have erred somewhere else. Is this code inside another method? Show us the code of that method and how you call it. – RaminS Mar 28 '16 at 23:47
  • Just ran this, it works fine. Perhaps more context is needed. – Ceelos Mar 28 '16 at 23:48
  • It lets me run the first if else statement but it skips over the second if else and ends the program – carlie Mar 28 '16 at 23:52

2 Answers2

3

The problem is

because the Scanner#nextInt method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner#nextLine

Solution

Either fire a blank nextLine() to consume the newline or change your nextInt() to a nextLine() and parse it as an integer.

Further details are located here: Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

Community
  • 1
  • 1
Keno Clayton
  • 1,670
  • 1
  • 12
  • 20
0

I try to run your code and i found a line which is :

cheeseburgerParts a = new cheeseburgerParts();

I remove this line then program compile without any error and all the thing works perfectly. if you are really using the line for some reason then check that class, That class may have some error. you should use while instead of else if so many times. hope this help.