【问题标题】:Scanning multiple lines using single scanner object使用单个扫描仪对象扫描多行
【发布时间】:2014-05-20 17:21:25
【问题描述】:

我是 java 新手,所以如果这听起来对你来说绝对愚蠢,请不要评价

好的,我如何使用单个扫描仪对象输入它

5

你好,你好吗

欢迎来到我的世界

6 7

给那些建议的人

scannerobj.nextInt->nextLine->nextLine->nextInt->nextInt,,,

检查一下,它不起作用!!!

谢谢

【问题讨论】:

  • 它适用于 scannerobj.nextInt->nextLine->nextLine->nextInt->nextLine->nextInt->nextLine.. 看看!
  • 请分享您的代码。
  • 请给出一个通用的答案含义,可用于任何多行集合!!!
  • 而不是这个创建单独的方法来打印值并在那里编写扫描仪代码。然后调用循环中的方法和你的数据
  • @RafaEl 是的,当且仅当您为输入部分添加额外的 nextLine 时,它​​才有效

标签: java input output java.util.scanner


【解决方案1】:

默认情况下,扫描仪使用空格作为分隔符。为了使用 forEachRemaining 逐行扫描,请将扫描仪分隔符更改为如下所示的行。

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    scanner.useDelimiter("\n");
    scanner.forEachRemaining(System.out::println);
}

【讨论】:

    【解决方案2】:

    也许我们可以使用这种方法:

    for(int i = 0; i < n; i++)
       {
           Scanner sc1 = new Scanner(System.in);
           str0[i] = sc1.nextLine();
           System.out.println(str0[i]);
       }
    

    也就是说,我们每次在读取 nextLine 之前创建扫描仪对象。 :)

    【讨论】:

      【解决方案3】:

      你也可以只用 lambda 试试:

      public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          scanner.forEachRemaining(input -> System.out.println(input));
      }
      

      【讨论】:

        【解决方案4】:
        public class Sol{
        
            public static void main(String[] args) {
        
                Scanner sc = new Scanner(System.in); 
        
               while(sc.hasNextLine()){
        
                   System.out.println(sc.nextLine());
               }
        
            }
        }
        

        【讨论】:

          【解决方案5】:

          试试这个代码

          Scanner  in    = new Scanner(System.in);
          
          System.out.printf("xxxxxxxxxxxxxxx ");        
          String[] input = new String[in.nextInt()];
          
          for (int i = 0; i < input.length; i++) {
              input[i] = in.nextLine();
          }
          
          for (String s : input) {
              System.out.println(s);
          }
          

          【讨论】:

          • 那将无法正常工作,因为in.nextInt() 使用 int 值,而不是换行符。因此,您的第一次 for 循环迭代将立即将该线路制动写入您的输入 [0]。
          【解决方案6】:
          public static void main(String[] args) {
              Scanner  in    = new Scanner(System.in);
          
              System.out.printf("Please specify how many lines you want to enter: ");        
              String[] input = new String[in.nextInt()];
              in.nextLine(); //consuming the <enter> from input above
          
              for (int i = 0; i < input.length; i++) {
                  input[i] = in.nextLine();
              }
          
              System.out.printf("\nYour input:\n");
              for (String s : input) {
                  System.out.println(s);
              }
          }
          

          示例执行:

          Please specify how many lines you want to enter: 3
          Line1
          Line2
          Line3
          
          Your input:
          Line1
          Line2
          Line3
          

          【讨论】:

          • 谢谢,对这一行加一分in.nextLine(); //consuming the &lt;enter&gt; from input above
          猜你喜欢
          • 1970-01-01
          • 2013-06-05
          • 1970-01-01
          • 2014-10-26
          • 1970-01-01
          • 2017-05-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多