我使用扫描器方法nextInt()和nextLine()读取输入。

它是这样的:

System.out.println("Enter numerical value");    
int option;
option = input.nextInt(); // Read numerical value from input
System.out.println("Enter 1st string"); 
String string1 = input.nextLine(); // Read 1st string (this is skipped)
System.out.println("Enter 2nd string");
String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value)

问题是在输入数值之后,第一个input.nextLine()被跳过,第二个input.nextLine()被执行,因此我的输出看起来像这样:

Enter numerical value
3   // This is my input
Enter 1st string    // The program is supposed to stop here and wait for my input, but is skipped
Enter 2nd string    // ...and this line is executed and waits for my input

我测试了我的应用程序,看起来问题在于使用input.nextInt()。如果我删除它,那么string1 = input.nextLine()和string2 = input.nextLine()都按照我想要的方式执行。


当前回答

因为nextXXX()方法不读取换行符,除了nextLine()。我们可以在读取任何非字符串值(在这种情况下是int)后跳过换行符,使用scanner.skip()如下所示:

Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
sc.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
System.out.println(x);
double y = sc.nextDouble();
sc.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
System.out.println(y);
char z = sc.next().charAt(0);
sc.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
System.out.println(z);
String hello = sc.nextLine();
System.out.println(hello);
float tt = sc.nextFloat();
sc.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
System.out.println(tt);

其他回答

要解决这个问题,只需创建scan. nextline(),其中scan是Scanner对象的一个实例。例如,我使用一个简单的HackerRank问题来解释。

package com.company;
import java.util.Scanner;

public class hackerrank {
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int i = scan.nextInt();
    double d = scan.nextDouble();
    scan.nextLine(); // This line shall stop the skipping the nextLine() 
    String s = scan.nextLine();
    scan.close();



    // Write your code here.

    System.out.println("String: " + s);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);
}

}

nextLine()将直接将enter读取为空行,而不等待文本。

简单的解决方案是添加一个额外的扫描器来消耗空行:

System.out.println("Enter numerical value");    
int option;
option = input.nextInt(); // Read numerical value from input
input.nextLine();
System.out.println("Enter 1st string"); 
String string1 = input.nextLine(); // Read 1st string (this is skipped)
System.out.println("Enter 2nd string");
String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value)
 Scanner scan = new Scanner(System.in);
    int i = scan.nextInt();
    scan.nextLine();//to Ignore the rest of the line after  (integer input)nextInt()
    double d=scan.nextDouble();
    scan.nextLine();
    String s=scan.nextLine();
    scan.close();
    System.out.println("String: " + s);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);

关于java.util.Scanner的这个问题似乎有很多问题。我认为一个更可读/惯用的解决方案是调用scanner.skip("[\r\n]+")在调用nextInt()后删除任何换行符。

编辑:正如下面提到的@PatrickParker,如果用户在数字后输入任何空白,这将导致无限循环。关于更好的skip模式,请参阅他们的回答:https://stackoverflow.com/a/42471816/143585

为了避免这个问题,请使用nextLine();紧接在nextInt()之后;因为它有助于清除缓冲区。当你按ENTER时,nextInt();不会捕获新行,因此稍后将跳过Scanner代码。

Scanner scanner =  new Scanner(System.in);
int option = scanner.nextInt();
scanner.nextLine(); //clearing the buffer