Java Input not working (Beginner)

两盒软妹~` 提交于 2020-01-11 10:21:49

问题


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");
                    }

回答1:


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.




回答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.




回答3:


Your code has three braces missing. Arraylist have to declared like this

 ArrayList<class> list = new ArrayList<class>();

If you want an arraylist of integers

ArrayList<Integers> in = new ArrayList<Integers>();


来源:https://stackoverflow.com/questions/29238680/java-input-not-working-beginner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!