扫描仪在使用next()或nextfoo()后跳过nextline()?

jmp7cifd  于 2021-06-30  发布在  Java
关注(0)|答案(21)|浏览(361)

我用的是 Scanner 方法 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() 按我的意愿执行。

r6vfmomb

r6vfmomb16#

使用此代码可以解决您的问题。

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)
ax6ht2ek

ax6ht2ek17#

如果我期望一个非空的输入

public static Function<Scanner,String> scanLine = (scan -> {
    String s = scan.nextLine();
    return( s.length() == 0 ? scan.nextLine() : s );
  });

用于上述示例:

System.out.println("Enter numerical value");    
int option = input.nextInt(); // read numerical value from input

System.out.println("Enter 1st string"); 
String string1 = scanLine.apply( input ); // read 1st string
System.out.println("Enter 2nd string");
String string2 = scanLine.apply( input ); // read 2nd string
dluptydi

dluptydi18#

要解决此问题,只需执行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);
}

}

qcbq4gxm

qcbq4gxm19#

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

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

toiithl620#

在我的一个用例中,我有这样一个场景:读取一个字符串值,后跟几个整数值。我必须使用“for/while循环”来读取值。而上述建议在这种情况下都不起作用。
使用 input.next() 而不是 input.nextLine() 解决了这个问题。希望这对处理类似情况的人有所帮助。

eulz3vhy

eulz3vhy21#

我想我去派对已经很晚了。。
如前所述,呼叫 input.nextLine() 得到int值后,问题就迎刃而解了。你的代码不起作用的原因是因为你的输入(输入int的地方)中没有其他东西可以存储 string1 . 我只想对整个主题多讲一点。
将nextline()看作scanner类中nextfoo()方法中的奇数。让我们举个简单的例子。。假设我们有两行代码,如下所示:

int firstNumber = input.nextInt();
int secondNumber = input.nextInt();

如果我们输入下面的值(作为一行输入)
54 234
我们的价值 firstNumber 以及 secondNumber 变量分别变为54和234。这样做的原因是,当nextint()方法接受值时,不会自动生成换行符(即\n)。它只需要“next int”就可以继续了。除nextline()外,其余的nextfoo()方法也是如此。
nextline()在获取一个值后立即生成一个新行提要;这就是@rohitjain所说的新行提要“已消耗”的意思。
最后,next()方法只取最近的字符串,而不生成新行;这使它成为在同一行中获取单独字符串的首选方法。
我希望这有帮助。。快乐的编码!

相关问题