1

I'm passing the input to the java program using the below command:

java Main - <"input.txt" > "output.txt"

by using this method the inputs are only received by first method (main)

Code:

import java.util.Scanner;
import java.util.Arrays;

class Main {
    public static int read() {
        Scanner input = new Scanner(System.in);
        int num2 = input.nextInt();
        System.out.println(num2);
        return num2;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num1 = input.nextInt();
        System.out.println(num1);
        read();
    }
}

Input input.txt contents:

1
2

Expected Output:

1
2

Actual Output

1

Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor(Scanner.java:937)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at Main.read(main.java:7)
    at Main.main(main.java:16)
Holger
  • 285,553
  • 42
  • 434
  • 765
DilLip_Chowdary
  • 841
  • 4
  • 18
  • What output do you expect? What do you get? – tgdavies Jan 28 '23 at 07:37
  • It works if you pass the scanner as a parameter to `read` instead of creating a new one, but I'm not sure why that is. – Tim Moore Jan 28 '23 at 11:16
  • 1
    Does this answer your question? [How to use multiple Scanner objects on System.in?](https://stackoverflow.com/questions/4232588/how-to-use-multiple-scanner-objects-on-system-in) – Tim Moore Jan 28 '23 at 23:42

1 Answers1

1

This can be fixed by reusing a single instance of Scanner, rather than creating a new instance in the read method:

import java.util.Scanner;
import java.util.Arrays;

class Main {
    public static int read(Scanner input) {
        int num2 = input.nextInt();
        System.out.println(num2);
        return num2;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num1 = input.nextInt();
        System.out.println(num1);
        read(input);
    }
}

This produces the expected output, and does not throw an exception.

Tim Moore
  • 8,958
  • 2
  • 23
  • 34
  • How can we do that without changing the code – DilLip_Chowdary Jan 28 '23 at 13:44
  • @DilLip_Chowdary the code is buggy, I don't think there's a workaround. – Tim Moore Jan 28 '23 at 23:42
  • We're able to run the above command successfully with manual inputs passing, getting this issue with only when passed with cli using a file. – DilLip_Chowdary Jan 30 '23 at 05:04
  • 1
    @DilLip_Chowdary code that works in one legitimate use case but not in the other is still broken. Your code happens to work when you place the numbers into two lines and wait between inserting the two lines, but it would also break when entering the lines manually when you copy & paste two lines at once (or hypothetically enter the two lines too fast). It also breaks when entering the numbers in one line. The takeaway is, `Scanner` is buffered and using multiple `Scanner` for the same source can case data loss or inconsistencies. – Holger Apr 28 '23 at 08:00