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)