I'm trying to solve a competitive coding problem and it works fine when i execute it on eclipse or on the command prompt, but when i uploaded the solution on the website it didn't execute and threw noSuchElementException at the line where i first took the input from the user. I've added the part of code which is causing the problem.
I've tried to execute it on different online compilers with java 8 compiler version but it still throws the same error. I've also tried to use BufferedReader but for some reason the code prints value of k as -1.
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
if(!sc.hasNext()){
System.out.println("hasNext returns false");
}
int k=sc.nextInt();
System.out.println(k);
}
}
output:
hasNext returns false
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 Solution.main(Solution.java:9)