【问题标题】:How to use hasNext() from the Scanner class?如何使用 Scanner 类中的 hasNext()?
【发布时间】:2015-12-18 11:51:29
【问题描述】:

输入格式

stdin(System.in)读取一些未知的n行输入,直到到达EOF;每行输入都包含一个非空字符串。

输出格式

对于每一行,打印行号,后跟一个空格,然后是作为输入接收到的行内容:

样本输出

Hello world
I am a file
Read me until end-of-file.  

这是我的解决方案。问题是我无法继续直到 EOF。 但输出只是:

Hello world

这是我的代码:

public class Solution {

    public static void main(String[] args) {
        check(1);  // call check method
    }

    static void check(int count) {          
        Scanner s = new Scanner(System.in);
        if(s.hasNext() == true) {
            String ns = s.nextLine();
            System.out.println(count + " " + ns);
            count++;
            check(count);
        }
    } 
}

【问题讨论】:

  • if(s.hasNext()==true){ 只计算一次。使用while
  • 你为什么首先使用递归?

标签: java java.util.scanner eof


【解决方案1】:

您的代码不起作用,因为您在每次递归调用中都创建了一个新的Scanner 对象。 无论如何,您不应该为此使用递归,而是迭代地进行。

迭代版本

public class Solution {

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        int count = 1;

        while(s.hasNext()) {
            String ns = s.nextLine();
            System.out.println(count + " " + ns);
            count++;
        }
    }
}

递归版本

public class Solution {

    private Scanner s;

    public static void main(String[] args) {

        s = new Scanner(System.in); // initialize only once
        check(1);
    }

    public static void check(int count) {
        if(s.hasNext()) {
            String ns = s.nextLine();
            System.out.println(count + " " + ns);
            check(count + 1);
        }
    }   
}

【讨论】:

    【解决方案2】:

    改变

    if (s.hasNext() == true) {
        String ns = s.nextLine();
        System.out.println(count + " " + ns);
        count++;
        System.out.print(count);
        check(count);
    }
    

    到:

    while (s.hasNext()) {
        String ns = s.nextLine();
        System.out.println(count + " " + ns);
        count++;
        System.out.print(count);
        check(count);
    }
    

    while 循环一直持续到数据存在,而if 只检查一次。

    【讨论】:

    • 是的,谢谢,但我正在递归调用检查函数以获取递增值。
    【解决方案3】:

    Scanner 有点像BufferedReader(我不是在谈论继承之类的东西。我是说他们都有缓冲区。Scanner 只有一个小缓冲区)。因此,在控制台中输入文本后,这些文本是来自 System.inread(),并存储在 Scanner 内的缓冲区中。

    public static void main(String[] args) {          
        Scanner s1 = new Scanner(System.in);
        s1.hasNext();
        Scanner s2 = new Scanner(System.in);
        while(true){
            System.out.println("Read line:: " + s2.nextLine());
        }
    }
    

    Scanner 中使用以下输入:

    第 1 行
    第 2 行
    第 3 行
    第 4 行

    你会得到输出:

    读取行::e 1
    阅读 line:: line 2
    读取 line:: line 3
    读取 line:: line 4

    我想你可能知道这个输出的原因。第一行的一些字符在Scanner s1 中。因此不要创建 2 个Scanners 来接收来自同一个Stream 的输入。

    您可以按如下方式更改代码以获得所需的输出。

    private static Scanner s;
    
    public static void main(String[] args) {
        s = new Scanner(System.in);
        check(1);  // call check method
    }
    
    static void check(int count) {
        if (s.hasNextLine()) {
            String ns = s.nextLine();
            System.out.println(count + " " + ns);
            count++;
            check(count);
        }
    }
    

    在逐行阅读时,您可以使用s.hasNextLine() 而不是s.hasNext()

    不需要使用s.hasNextLine()==true,因为当且仅当s.hasNextLine()true 时,该语句才会是true

    您可以在 Windows 系统中使用 Ctrl+Z 和在 Unix 中使用 Ctrl+DEOF 字符赋予控制台。据我所知,您不能使用 NetBeans 的输出窗口发送EOF 字符。

    【讨论】:

      【解决方案4】:

      如果需要使用递归,您可以使用辅助函数:

      static void check(int count) {          
          Scanner s = new Scanner(System.in);
          check(count, s);
      } 
      
      static void check(int count, Scanner scanner) {
          if(!scanner.hasNext()) {
              return;
          }
          String ns = scanner.nextLine();
          System.out.println(count + " " + ns);
          check(++count, scanner);
      }
      

      注意new Scanner(System.in) 只被调用一次。

      【讨论】:

        【解决方案5】:
        public static void main(String[] args) {
        
            List<String> eol = new ArrayList<String>();
            Scanner in=new Scanner(System.in);
            int t=1;
            while(in.hasNext()){
        
                eol.add(in.nextLine());
            }
        
            for (String i:eol){
        
                System.out.println(t+" "+i);
                t++;
            }
        
        }
        

        【讨论】:

          【解决方案6】:
          public static void main(String[] args) {
              Scanner scan = new Scanner(System.in);
              int count = 1;
              while(scan.hasNext()){
                  System.out.println(count++ + " " + scan.nextLine());            
              }
          }
          

          【讨论】:

            【解决方案7】:

            这是没有错误的程序。

            import java.util.Scanner;
            
            public class Solution {
            
                public static void main(String[] args) {
                    Scanner scan = new Scanner(System.in);
                    int count = 1;
                    while(scan.hasNext()) {
                        String s = scan.nextLine();
                        System.out.println(count + " " + s);
                        count++;
                    }
                }
            }
            

            【讨论】:

              【解决方案8】:
              Scanner scanner = new Scanner(System.in);
              int i = 1;
              while(scanner.hasNext()) {
                System.out.println(i + " " + scanner.nextLine());
                i++;
              }
              

              【讨论】:

              • 虽然欢迎使用此代码 sn-p,并且可能会提供一些帮助,但它会是 greatly improved if it included an explanation of 如何解决这个问题。没有这个,你的回答就没有多少教育价值了——记住你是在为未来的读者回答这个问题,而不仅仅是现在提问的人!请edit您的答案添加解释,并说明适用的限制和假设。
              【解决方案9】:

              使用while代替if,但递归版本更好。这里是迭代版本:

              import java.io.*;
              import java.util.*;
              
              public class Solution {
              
                  public static void main(String[] args) {
                      /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
                      Scanner sc=new Scanner(System.in);
                      {
                          int i=0;
              
                        while(sc.hasNext()==true)
                          {
                              i=i+1;
                               String s=sc.nextLine();
                              System.out.println(i+" "+s);
              
                          };
                      }
                  }
              }
              

              【讨论】:

                【解决方案10】:

                java.util.Scanner.hasNext() 基本上可以帮助您阅读输入。如果此 Scanner 在其输入中有任何类型的另一个标记,则此方法返回 true。否则返回 false。 检查下面的代码sn-p,

                 while(sc.hasNext()) {
                      System.out.println(i + " " + sc.nextLine());
                      i++;
                 }
                

                您可以在下面的链接中找到完整的代码,

                https://github.com/hetalrachh/HackerRank/blob/master/Practice/Java/Introduction/EndOfFile.java

                【讨论】:

                  【解决方案11】:

                  你可以这样尝试得到想要的结果:

                  公共类 EOF {

                  public static void main(String[] args) {
                      Scanner scan = new Scanner(System.in);
                      for(int i =1;scan.hasNext();i++) {
                          String line = scan.nextLine();
                          System.out.println(i + " " + line);
                      }
                      scan.close();
                  }
                  

                  }

                  【讨论】:

                    【解决方案12】:
                    public class Solution {
                    
                        
                        public static void main(String[] args) {
                        Scanner scan = new Scanner(System.in);
                        for(int i=1; scan.hasNext() ; 
                        System.out.println(i++ +" "+scan.nextLine()));
                            
                          
                        }
                    }
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 2019-12-27
                      • 2013-03-28
                      • 2020-03-10
                      • 2012-05-16
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多