Java 扫描仪在使用next()或nextFoo()后跳过nextLine()?

Java 扫描仪在使用next()或nextFoo()后跳过nextLine()?,java,io,java.util.scanner,Java,Io,Java.util.scanner,我正在使用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(); // R

我正在使用
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)
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.nextLine()
,执行第二个
input.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)
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()
都会按照我的要求执行。

问题在于input.nextLine()方法-它只读取int值。因此,当您继续使用input.nextLine()读取时,您将收到“\n”Enter键。因此,要跳过此操作,必须添加输入.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();
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();

问题在于input.nextInt()方法-它只读取int值。因此,当您继续使用input.nextLine()读取时,您将收到“\n”Enter键。因此,要跳过此操作,必须添加输入.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();
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();

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


相反,在
input.nextLine()
之后立即使用
input.nextLine()
这是因为当您输入一个数字,然后按enter键时,
input.nextLine()
只使用该数字,而不使用“行尾”。当执行
input.nextLine()
时,它会使用第一次输入时仍在缓冲区中的“行尾”

相反,在
input.nextLine()
之后立即使用
input.nextLine()
,这是因为该方法不会读取通过点击“Enter”创建的输入中的换行符,因此对的调用在读取该换行符后返回

使用
Scanner.nextLine
after或任何
Scanner.nextFoo
方法(除了
nextLine
本身)时,您会遇到类似的行为

解决方法:

  • 在每个
    Scanner.nextLine
    之后放置一个
    Scanner.nextLine
    调用或
    Scanner.nextFoo
    以使用该行的其余部分,包括换行符

  • 或者,更好的方法是通过Scanner.nextLine读取输入,并将输入转换为所需的正确格式。例如,您可以使用方法转换为整数

这是因为该方法不会读取通过点击“Enter”创建的输入中的换行符,因此对的调用在读取该换行符后返回

使用
Scanner.nextLine
after或任何
Scanner.nextFoo
方法(除了
nextLine
本身)时,您会遇到类似的行为

解决方法:

  • 在每个
    Scanner.nextLine
    之后放置一个
    Scanner.nextLine
    调用或
    Scanner.nextFoo
    以使用该行的其余部分,包括换行符

  • 或者,更好的方法是通过Scanner.nextLine读取输入,并将输入转换为所需的正确格式。例如,您可以使用方法转换为整数


    • 之所以这样做是因为
      input.nextInt()不捕获换行符。通过添加
      input.nextLine(),您可以像其他建议的那样做下方。
      或者,您可以采用C#风格,将下一行解析为整数,如下所示:

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

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

      这样做是因为
      input.nextInt()不捕获换行符。通过添加
      input.nextLine(),您可以像其他建议的那样做下方。
      或者,您可以采用C#风格,将下一行解析为整数,如下所示:

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

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

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


      编辑:正如@PatrickParker在下面提到的,如果用户在数字后面输入任何空格,这将导致无限循环。查看他们的答案,以获得更好的skip模式:

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


      编辑:正如@PatrickParker在下面提到的,如果用户在数字后面输入任何空格,这将导致无限循环。查看他们的答案,以获得更好的模式来使用skip:

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

      修改代码:

      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();
      }
      

      不要使用
      input.nextLine()
      而是使用
      input.next()
      ,这样可以解决问题

      修改代码:

      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();
      
      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)
      

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

      int i = new Scanner(System.in).nextInt();
      
      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)
      
      TL;DR
      在每次调用
      scanner.newLine()
      之前使用
      scanner.skip(\\R”)
      (因为
      skip
      使用正则表达式,其中
      \R
      表示行分隔符),该调用在以下时间之后执行:

      • scanner.next()
      • scanner.next*键入*()
        方法,如
        scanner.nextInt()
      或更安全的变体:
      
      
      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
      
      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)
      
      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();
      
      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);
      }