2

For some reason, my code will not accept input on the last line "What would you like to order: "

Could anyone tell me what my error is here? It is compiling correctly and everything. I am only a beginner so please tell me in basic terms.

import java.util.Scanner;
import java.util.*;

class RestaurantMain {
    public static void main(String[] args)
    {

        //Create an array list
        ArrayList menu = new ArrayList();

        //Variables//
        int choice;
        int customerChoice;
        boolean trueFalse;
        int restart = 0;
        String choice2;
        String addItems = "";
        int menuCount = 0;
        int indexCount = 0;
        String item = "";

        //Import input device
        Scanner in = new Scanner(System.in);

        ArrayList theMenu = new ArrayList();

        System.out.println("Welcome to the Cooper's restaurant system!");
        System.out.println("How can I help?");
        System.out.println("");
        System.out.println("1. Customer System");
        System.out.println("2. Management System");
        System.out.println("");
        System.out.println("");
        System.out.print("Which option do you choose: ");
        choice = in.nextInt();

            if (choice == 1) {
                System.out.println("Our menu's are as follows:");
                System.out.println("");
                System.out.println("1. Drinks");
                System.out.println("2. Starters");
                System.out.println("3. Mains");
                System.out.println("4. Desserts");
                System.out.println("");
                System.out.println("Please note - You MUST order 5 items.");
                System.out.println("");
                System.out.print("What menu would you like to follow? ");
                customerChoice = in.nextInt();

                    if (customerChoice == 1) {
                        System.out.println("Drinks Menu");
                            System.out.println("Would you like to order? ");
                            choice2 = in.nextLine();
                                if (choice2 == "yes") {
                                    System.out.println("Please enter the amount of items you want to order: ");
                                    while (indexCount <= menuCount);
                                        System.out.println("Please enter your item: ");
                                        item = in.nextLine(); {
                                        theMenu.add(item);
                                    }
                                    }

                    }
                    if (customerChoice == 2) {
                        System.out.println("Starters Menu");
                    }
                    if (customerChoice == 3) {
                        System.out.println("Mains menu");
                    }
                    if (customerChoice == 4) {
                        System.out.println("Desserts Menu");
                    }
3
  • 5
    I'm not sure if it's the only issue, but you're trying to use == to compare strings. Use .equals() instead. Commented Mar 24, 2015 at 17:06
  • 3
    Even more, use "yes".equals(choice2) instead of choice2.equals("yes"). It's a good practice which save you from wild NullPointerException :) Commented Mar 24, 2015 at 17:09
  • Thanks guys! Hope you both have a good day! Commented Mar 24, 2015 at 17:14

2 Answers 2

4

You need to call in.nextLine() right after the line where you call in.nextInt() The reason is that just asking for the next integer doesn't consume the entire line from the input, and so you need skip ahead to the next new-line character in the input by calling in.nextLine()

customerChoice = in.nextInt();
in.nextLine();

This pretty much has to be done each time you need to get a new line after calling a method that doesn't consume the entire line. Consider using a BufferedReader object instead!

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int integer = Integer.parseInt(reader.readLine());

This will throw the same errors that Scanner.nextInt() does if the input can't be parsed as an integer.

Regarding your comment about errors, there is one:

while (indexCount <= menuCount);
System.out.println("Please enter your item: ");
item = in.nextLine(); {
 theMenu.add(item);
}

}

Should instead be like the following:

while(indexCount <= menuCount){
    System.out.println("Please enter your item: ");
    item = in.nextLine();
    theMenu.add(item);
}

Also, it isn't strictly necessary, but I suggest that you do declare the ArrayList's generic type when instantiating the list, so that further calls to theMenu.get() don't need to be casted to a String.

ArrayList<String> theMenu = new ArrayList<String>();

When comparing strings, ensure that you use the str.equals("string to compare with") method, instead of the equality operator (==). Therefore for example, choice2 == "yes" should instead be choice2.equals("yes"). Using equalsIgnoreCase instead of equals would ignore case differences, which may be useful in this situation.

Sign up to request clarification or add additional context in comments.

3 Comments

Also can I ask, do you see any beginner errors in my code that I cane improve upon? Many thanks! Have a good day!
There is one error, see my edited answer for info! @CoopersInc
I can't thank you enough @SamTebbs33! Your information is greatly appreciated and will go well towards my final exam + management system :) Reading through this will help a lot when needed. Is there any way to repay a favour?
2

insted of in.nextLine(); function you just try another scanner functions like 'in.next()'. just R&D with the methods that already give the JVM itself. you just use correct the logic and use equal() or equlIgnoreCase() methods insted of "=" operator.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.