Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use hasNext() from the Scanner class?

Input Format

Read some unknown n lines of input from stdin(System.in) until you reach EOF; each line of input contains a non-empty String.

Output Format

For each line, print the line number, followed by a single space, and then the line content received as input:

Sample Output

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

Here is my solution. The problem being I am not able to proceed till EOF. But the output is just:

Hello world

Here is my code:

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);
        }
    } 
}
like image 312
Harshit Juneja Avatar asked Dec 18 '15 11:12

Harshit Juneja


3 Answers

Your code does not work because you create a new Scanner object in every recursive call. You should not use recursion for this anyways, do it iteratively instead.

Iterative version

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

Recursive version

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);
        }
    }   
}
like image 151
Willi Mentzel Avatar answered Oct 03 '22 11:10

Willi Mentzel


Change

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

to:

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

while loops continues until the data exists, where as if checks for only once.

like image 45
Suresh Atta Avatar answered Oct 03 '22 12:10

Suresh Atta


Scanner is kind of a BufferedReader (I'm not telling about inheritance or something. I'm telling they both have buffers. Scanner has just a small one). So after you enter text in the Console, those are read() from System.in and stored in the buffer inside the 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());
    }
}

Use the following input to the Scanner:

line 1
line 2
line 3
line 4

You will get the output:

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

I think you might know the reason to this output. Some characters of the first line are in the Scanner s1. Therefore don't create 2 Scanners to take input from same Stream.

You can change your code as follows to get required output.

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

You can use s.hasNextLine() instead of s.hasNext() as you are reading line by line.

No need to use s.hasNextLine()==true as that statement will be true if and only if s.hasNextLine() is true.

You can give EOF character to the console using Ctrl+Z in Windows system and Ctrl+D in Unix. As I know, you can't send EOF character using the output window of NetBeans.

like image 33
Ramesh-X Avatar answered Oct 03 '22 13:10

Ramesh-X