0

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)

3 Answers3

0

Here you are checking sc.hasNext() and it will print "hasNext returns false" but after this you are again getting the nextInt() which wont be there since in online compiler you are not able to pass arguments in run time.

try this,

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        if(!sc.hasNext()){
            System.out.println("hasNext returns false");          
        } else {
        int k=sc.nextInt(); 
         System.out.println(k);
        }
      }
Arun Prasat
  • 340
  • 1
  • 9
  • Thank you for answering, but the else part did not execute it printed the same message and terminated. – ankur jayant Jul 12 '19 at 13:24
  • Yes you cant pass arguments in all the online compilers, try this below URL https://www.jdoodle.com/online-java-compiler. Add your code here and pass the values in stdin and commandline arg. It will work – Arun Prasat Jul 12 '19 at 13:42
0

I think you just don't have a standard input stream at hand if you're using some online compilers. Just simulate your input like this:

Scanner sc = new Scanner("42");

Although you checked that hasNext() returns false, you are still trying to read the next int which leads to the exception. There is a code comment above java.util.Scanner.throwFor() which seems to confirm this:

// If we are at the end of input then NoSuchElement;
// If there is still input left then InputMismatch
Matthias
  • 7,432
  • 6
  • 55
  • 88
  • Thank you for answering, it worked. You were right System.in is not working in online compiler. Could you suggest me a way how should i modify the code so it would take the input which the online compiler would pass it. Now it is just printing 42 on the output – ankur jayant Jul 12 '19 at 13:26
0

Maybe you should use a static method, like:

nextInt();
nextLine();
nextDouble();
nextBoolean();
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Answer
  • 1
  • 2