Java 无法从扫描仪获取一个特定输入

Java 无法从扫描仪获取一个特定输入,java,Java,我是个新手。我知道我的代码很乱。我将致力于添加评论等 try // get customer's address { System.out.println("\nPlease type in your shipping address."); System.out.println ("This way you can receive what you have ordered."); System.out.println ("In this format:

我是个新手。我知道我的代码很乱。我将致力于添加评论等

    try // get customer's address
    {
    System.out.println("\nPlease type in your shipping address.");
    System.out.println ("This way you can receive what you have ordered.");
    System.out.println ("In this format: Street, City, State, Zipcode\n");
    customerAddress = input.nextLine();
    }
    catch (Exception e)
            {
                System.out.println("You need to enter in an address.");
            }

    try // get customer's telephone number
    {
        System.out.println("Please enter in your telephone number:\n");
        phoneNumber = input.nextLine();
    }
    catch (Exception e)
    {
        System.out.println("You need to enter in a phone number.");
    }
我可以从电话号码中获得输入,但程序似乎跳过了customerAddress输入

下面是我在命令提示符中得到的内容。请注意,我可以在电话号码下输入数据,但没有机会将其放入地址部分

请输入您的送货地址。
这样您就可以收到您订购的商品。
此格式:街道、城市、州、Zipcode

请输入您的电话号码:

123457890


是否有任何逻辑错误可能导致其跳过?

您的帖子中没有显示的任何其他内容都可能导致缓冲区中保留一个新的错误行。一个更健壮的选项是在nextLine返回空字符串时循环。

如果您正在读取更多数据,请使用类似于
input.nextInt()的Scaner然后它将只读取一个int。
一种解决方案是添加
input.nextLine()并且它可能会工作

解决方案2: 使用
BufferedReader

        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));

        try{
            System.out.println("\nPlease type in your shipping address.");
            System.out.println ("This way you can receive what you have ordered.");
            System.out.println ("In this format: Street, City, State, Zipcode\n");

            customerAddress = bufferRead.readLine();
        }catch (Exception e){
            System.out.println("You need to enter in an address.");
        }

        try {
             System.out.println("Please enter in your telephone number:\n");
             phoneNumber =  bufferRead.readLine();
        }catch (Exception e){
             System.out.println("You need to enter in a phone number.");
        }  


        System.out.println(customerAddress + " " + phoneNumber);
查看是否使用BufferedReader获得输出


希望这能有所帮助。

在此之前您是否阅读过任何数字?如果是这样,只需添加input.nextLine();在phoneNumber=input.nextLine()之前的行中;接受上述评论作为答案:)。