public class a2 {
public static int read() {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
sc.close();
return num;
}
public static void out (int a, int b) {
System.out.println("Sum: " + (a+b));
System.out.println("Difference: " + (a-b));
System.out.println("Product: " + (a*b));
System.out.println("Quotient: " + ((double)a/(double)b));
System.out.println("Remainder: " + (a%b));
}
public static void main(String[] args) {
System.out.println("Please enter two integers!:");
int a = read();
int b = read();
out(a,b);
}
}
I do have a little understanding problem with my code. Everytime I run the code, I get this Error Message after I enter the first integer.
Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at a2.read(a2.java:6) at a2.main(a2.java:22)
I figured out, when I delete the "sc.close();" line or when I define one of the two variables as a constant it works perfectly fine. Could someone explain this to me?