我使用扫描器方法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()都按照我想要的方式执行。


当前回答

要解决这个问题,只需创建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);
}

}

其他回答

那是因为扫描仪。nextInt方法不读取通过按“Enter”创建的输入中的换行符,因此调用Scanner。nextLine在读取换行符后返回。

当您使用Scanner时,您将遇到类似的行为。在Scanner.next()或任何Scanner之后的nextLine。nextFoo方法(除了nextLine本身)。

处理:

Either put a Scanner.nextLine call after each Scanner.nextInt or Scanner.nextFoo to consume rest of that line including newline int option = input.nextInt(); input.nextLine(); // Consume newline left-over String str1 = input.nextLine(); Or, even better, read the input through Scanner.nextLine and convert your input to the proper format you need. For example, you may convert to an integer using Integer.parseInt(String) method. int option = 0; try { option = Integer.parseInt(input.nextLine()); } catch (NumberFormatException e) { e.printStackTrace(); } String str1 = input.nextLine();

使用input.next()而不是input.nextLine(),这应该可以解决问题。

修改代码:

public static Scanner input = new Scanner(System.in);

public static void main(String[] args)
{
    System.out.print("Insert a number: ");
    int number = input.nextInt();
    System.out.print("Text1: ");
    String text1 = input.next();
    System.out.print("Text2: ");
    String text2 = input.next();
}

为什么不使用一个新的扫描器每次读取?像下面。用这种方法你就不会直面你的问题。

int i = new Scanner(System.in).nextInt();

如果你想同时读取字符串和int,一个解决方案是使用两个扫描仪:

Scanner stringScanner = new Scanner(System.in);
Scanner intScanner = new Scanner(System.in);

intScanner.nextInt();
String s = stringScanner.nextLine(); // unaffected by previous nextInt()
System.out.println(s);

intScanner.close();
stringScanner.close();

这是因为当您输入一个数字然后按enter时,input.nextInt()只使用该数字,而不是“行结束”。当input. nextline()执行时,它使用仍然在第一个输入缓冲区中的“行尾”。

相反,在input.nextInt()之后立即使用input.nextLine()