为什么在 Java 中跳过整数输入后的字符串输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26626106/
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-08-11 02:58:44  来源:igfitidea点击:

Why string inputs after integer input gets skipped in Java?

java

提问by bhanu

I am trying to input values of certain string and integer variables in Java. But if I am taking the input of string after the integer, in the console the string input is just skipped and moves to the next input.

我正在尝试在 Java 中输入某些字符串和整数变量的值。但是,如果我在整数之后输入字符串,则在控制台中,字符串输入将被跳过并移动到下一个输入。

Here is the code

这是代码

String name1;
int id1,age1;

Scanner in = new Scanner(System.in);

//I can input name if input is before all integers

System.out.println("Enter id");
id1 = in.nextInt();


System.out.println("Enter name");       //Problem here, name input gets skipped
name1 = in.nextLine();

System.out.println("Enter age");
age1 = in.nextInt();

采纳答案by enrico.bacis

This is a common problem, and it happens because the nextIntmethod doesn't read the newline character of your input, so when you issue the command nextLine, the Scanner finds the newline character and gives you that as a line.

这是一个常见问题,发生这种情况是因为该nextInt方法不会读取您输入的换行符,因此当您发出命令时nextLine,扫描仪会找到换行符并将其作为一行显示。

A workaround could be this one:

一种解决方法可能是这样的:

System.out.println("Enter id");
id1 = in.nextInt();

in.nextLine();   // skip the newline character

System.out.println("Enter name");
name1 = in.nextLine();

Another way would be to always use nextLinewrapped into a Integer.parseInt:

另一种方法是始终使用nextLine包装成一个Integer.parseInt

int id1;
try {
    System.out.println("Enter id");
    id1 = Integer.parseInt(input.nextLine());
} catch (NumberFormatException e) {
    e.printStackTrace();
}
System.out.println("Enter name");
name1 = in.nextLine();

Why not just Scanner.next()?

为什么不只是Scanner.next()

I would not use Scanner.next()because this will read only the next token and not the full line. For example the following code:

我不会使用,Scanner.next()因为这只会读取下一个标记而不是整行。例如下面的代码:

System.out("Enter name: ");
String name = in.next();
System.out(name);

will produce:

将产生:

Enter name: Mad Scientist
Mad

It will not process Scientistbecause Madis already a completed token per se. So maybe this is the expected behavior for your application, but it has a different semantic from the code you posted in the question.

它不会处理Scientist,因为Mad本身已经是一个完整的令牌。所以也许这是您的应用程序的预期行为,但它与您在问题中发布的代码具有不同的语义。

回答by Rookie007

May be you try this way..

也许你试试这种方式..

Instead of this code

而不是这个代码

       System.out.println("Enter name");       //Problem here, name input gets skipped
       name1 = in.nextLine();

try this

尝试这个

       System.out.println("Enter name");      
       name1 = in.next();

回答by sTg

This is your updated working code.

这是您更新的工作代码。

 package myPackage;

    import java.util.Scanner;

    public class test {

        /**
         * @param args
         */
        public static void main(String[] args) {
            String name1;
            int id1,age1;

            Scanner in = new Scanner(System.in);

            //I can input name if input is before all integers

            System.out.println("Enter id");
            id1 = in.nextInt();


            System.out.println("Enter name");       //Problem here, name input gets skipped
            name1 = in.next();

            System.out.println("Enter age");
            age1 = in.nextInt();

        }

    }