如何使用 Scanner class 中的 hasNext()?

How to use hasNext() from the Scanner class?

输入格式

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

输出格式

对于每一行,打印行号,然后是单个 space,然后是作为输入接收的行内容:

示例输出

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) {
    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 只检查一次。

您的代码不起作用,因为您在每次递归调用中都创建了一个新的 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);
        }
    }   
}

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

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) 只调用一次。

Scanner 有点像 BufferedReader(我不是在谈论继承什么的。我是说它们都有缓冲区。Scanner 只有一个小缓冲区) .因此,在您在控制台中输入文本后,这些是 System.in 中的 read() 并存储在 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 使用以下输入:

line 1
line 2
line 3
line 4

您将得到输出:

Read line:: e 1
Read line:: line 2
Read line:: line 3
Read line:: line 4

我想您可能知道此输出的原因。第一行的一些字符在Scanner s1中。因此 不要 创建 2 Scanner 来从同一个 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+D 来为控制台提供 EOF 字符。据我所知,您不能使用 NetBeans 的输出 window 发送 EOF 字符。

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++;
    }

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

这是没有错误的程序。

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++;
        }
    }
}
Scanner scanner = new Scanner(System.in);
int i = 1;
while(scanner.hasNext()) {
  System.out.println(i + " " + scanner.nextLine());
  i++;
}

用while代替if,递归版本是better.Here是迭代版本:

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

            };
        }
    }
}

java.util.Scanner.hasNext() 基本上可以帮助您阅读输入。此方法 returns 如果此扫描器在其输入中具有任何类型的另一个标记,则为真。 Returns 否则为假。 检查下面的代码片段,

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

您可以在下面找到完整的代码 link,

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

您可以这样尝试以获得想要的结果:

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

}

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