java 正在跳过扫描仪 nextLine

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14898617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:46:10  来源:igfitidea点击:

Scanner nextLine is being skipped

java

提问by Reflex Donutz

So this is the code I am using:

所以这是我正在使用的代码:

System.out.println("Create a name.");
name = input.nextLine();
System.out.println("Create a password.");
password = input.nextLine();

But when it reaches this point it just says "Create a name." and "Create a password." both at the same time and then I have to type something. So it's basically skipping the Scanner parts where I need to type a String. After "Create a name." and "Create a password." is outprinted and I type then, both name and password are changing to what I typed in. How do I fix this?

但是当它到达这一点时,它只会说“创建一个名称”。和“创建密码”。两者同时进行,然后我必须输入一些内容。所以它基本上跳过了我需要输入字符串的扫描器部分。在“创建名称”之后。和“创建密码”。打印出来然后我输入,名称和密码都更改为我输入的内容。我该如何解决这个问题?

This is the full class. I am just testing so it isn't actually going to be a program:

这是全班。我只是在测试,所以它实际上不会成为一个程序:

package just.testing;

import java.util.Scanner;
public class TestingJava
{
    static int age;
    static String name;
    static String password;
    static boolean makeid = true;
    static boolean id = true;

    public static void main(String[] args){
        makeid(null);
        if(makeid == true){
            System.out.println("Yay.");
        }else{

        }
    }
    public static void makeid(String[] args){
        System.out.println("Create your account.");
        Scanner input = new Scanner(System.in);
        System.out.println("What is your age?");
        int age = input.nextInt();
        if(age<12){
            System.out.println("You are too young to make an account.");
            makeid = false;
            return;
        }
        System.out.println("Create a name.");
        name = input.nextLine();
        System.out.println("Create a password.");
        password = input.nextLine();
        return;
    }
}

And sorry for my bad grammar. I am not English so it is kinda hard for me to explain this.

对不起,我的语法不好。我不是英语,所以我很难解释这一点。

回答by zw324

The nextInt() ate the input number but not the EOLN:

nextInt() 吃掉了输入数字,但没有吃掉 EOLN:

Create your account.
What is your age?
123 <- Here's actually another '\n'

so the first call to nextLine() after create name accept that as an input.

所以在 create name 之后对 nextLine() 的第一次调用接受它作为输入。

System.out.println("Create a name.");
name = input.nextLine(); <- This just gives you "\n"

User Integer.parseInt(input.nextLine()) or add another input.nextLine() after reading the number will solve this:

用户 Integer.parseInt(input.nextLine()) 或在读取数字后添加另一个 input.nextLine() 将解决此问题:

int age = Integer.parseInt(input.nextLine());

or

或者

int age = input.nextInt();
input.nextLine()

Also see herefor a duplicated question.

另请参阅此处的重复问题。

回答by Sean Landsman

This doesnt actually tell you why the lines are being skipped, but as you're capturing names and passwords you could use Console:

这实际上并没有告诉您为什么跳过这些行,但是当您捕获名称和密码时,您可以使用 Console:

Console console = System.console();
String name = console.readLine("Create a name.");
char[] password = console.readPassword("Create a password.");
System.out.println(name + ":" + new String(password));

回答by RajputAdya

You can also use next() instead of nextLine(). I have tested it in eclipse. its working fine.

您也可以使用 next() 代替 nextLine()。我已经在 Eclipse 中对其进行了测试。它的工作正常。

回答by Priyank Yadav

next()will work but it will not read the string after space.

next()会工作,但它不会读取空格后的字符串。