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

xn1cxnb4  于 2021-08-20  发布在  Java
关注(0)|答案(20)|浏览(317)

我正在使用 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() 按照我的要求执行。

ruarlubt

ruarlubt1#

问题在于input.nextint()方法-它只读取int值。因此,当您继续阅读input.nextline()时,您将收到

3pvhb19x

3pvhb19x2#

为什么每次阅读都不使用新的扫描仪呢?如下图所示。使用这种方法,您将不会面对您的问题。

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

krugob8w3#

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

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

r9f1avp54#

作为 nextXXX() 方法不读 newline 除了 nextLine() . 我们可以跳过 newline 读过任何 non-string 价值观( 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);
jxct1oxe

jxct1oxe5#

sc.nextLine() 与解析输入相比,它的性能更好。因为就性能而言,这将是好的。

os8fio9y

os8fio9y6#

我想我参加晚会已经很晚了。。
如前所述 input.nextLine() 得到int值后,将解决您的问题。您的代码不起作用的原因是,您的输入(输入int的地方)中没有其他内容可存储 string1 . 我只想对整个主题多说一点。
将NestRoin()视为扫描器类中的NeXFoT()方法中的奇数。让我们举一个很快的例子。假设我们有两行代码,如下所示:

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

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

093gszye

093gszye7#

public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        scan.nextLine();
        double d = scan.nextDouble();
        scan.nextLine();
        String s = scan.nextLine();

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

vsnjm48y8#

如果我期望一个非空的输入
避免:
–如果以下输入被未经检查的 scan.nextLine() 作为解决办法
–仅部分读取行导致数据丢失,因为 scan.nextLine() 被替换为 scan.next() (输入:“Yeppie ya yeah”)
–  Exception 使用解析输入时抛出的 Scanner 方法(先读取,后解析)

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
at0kjp5o

at0kjp5o9#

使用两个扫描仪对象,而不是一个

Scanner input = new Scanner(System.in);
System.out.println("Enter numerical value");    
int option;
Scanner input2 = new Scanner(System.in);
option = input2.nextInt();
t3psigkw

t3psigkw10#

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

7kjnsjlb

7kjnsjlb11#

那是因为 Scanner.nextInt 方法不会读取通过单击“enter”创建的输入中的换行符,因此调用 Scanner.nextLine 读取该换行符后返回。
在使用时,您将遇到类似的行为 Scanner.nextLine 之后 Scanner.next() 或任何 Scanner.nextFoo 方法(除 nextLine 本身)。
解决方法:
要么放一个 Scanner.nextLine 每次之后都打电话 Scanner.nextIntScanner.nextFoo 使用该行的其余部分,包括换行符

int option = input.nextInt();
input.nextLine();  // Consume newline left-over
String str1 = input.nextLine();

或者,更好的是,通读输入 Scanner.nextLine 并将输入转换为所需的正确格式。例如,您可以使用 Integer.parseInt(String) 方法。

int option = 0;
try {
    option = Integer.parseInt(input.nextLine());
} catch (NumberFormatException e) {
    e.printStackTrace();
}
String str1 = input.nextLine();
blmhpbnm

blmhpbnm12#

问题在于input.nextint()方法-它只读取int值。因此,当您继续使用input.nextline()读取时,您将收到“\n”enter键。因此,要跳过此操作,必须添加input.nextline()。希望这一点现在已经清楚了。
试着这样做:

System.out.print("Insert a number: ");
int number = input.nextInt();
input.nextLine(); // This line you have to add (It consumes the \n character)
System.out.print("Text1: ");
String text1 = input.nextLine();
System.out.print("Text2: ");
String text2 = input.nextLine();
u2nhd7ah

u2nhd7ah13#

这是因为当你输入一个数字,然后按enter键, input.nextInt() 只消耗数字,而不是“行尾”。什么时候 input.nextLine() 执行时,它将使用第一个输入中仍在缓冲区中的“行尾”。
相反,使用 input.nextLine() 紧接着 input.nextInt()

nkhmeac6

nkhmeac614#

关于这个问题,我们似乎有很多疑问 java.util.Scanner . 我认为更具可读性/惯用的解决方案是 scanner.skip("[\r\n]+") 调用后删除任何换行符 nextInt() .
编辑:正如@patrickparker在下面提到的,如果用户在数字后面输入任何空格,这将导致无限循环。请参阅他们的答案,以获得更好的模式,以便与skip一起使用:https://stackoverflow.com/a/42471816/143585

9jyewag0

9jyewag015#

它这样做是因为 input.nextInt(); 没有捕捉到新线。您可以通过添加 input.nextLine(); 在下面
或者,您可以采用c#风格,将下一行解析为整数,如下所示:

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

这样做同样有效,并且可以为您节省一行代码。

相关问题