I tried to solve a problem on CodeChef. The problem should be multiplying two numbers. This is the script:
import java.util.*;
import java.lang.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
int test;
Scanner sc = new Scanner(System.in);
test = sc.nextInt();
while(test!=0){
test = test - 1;
int nummer1;
int nummer2;
nummer1 = sc.nextInt();
nummer2 = sc.nextInt();
int nummer3;
nummer3 = nummer1 * nummer2;
System.out.println(nummer3);
}
}
}
What my program needs to do is: first, get the amount of test cases, and then, it should multiply nummer1 and nummer2 to make nummer3. For example:
INPUT
2
2 3
4 6
And the OUTPUT:
6
24
But the whole program is giving me a runtime error (NZEC). What am I doing wrong? It gives me this error:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:10)
I would do it this way:
Scanner sc = new Scanner(System.in);
int test = Integer.parseInt(sc.nextLine());
while (test-- > 0) {
String[] tmp = sc.nextLine().split("\\s+");
int result = Integer.parseInt(tmp[0]) * Integer.parseInt(tmp[1]);
System.out.println(result);
}
sc.close();