1

EDIT: This question was marked as duplicate I have searched everywhere I could including StachOverflow. I came across a lot of answers that could not resolve this issue. The only answer I came across was the

Scanner.hasNext();

While this gives me a "False" output, it still doesn't resolve the issue. Since I didn't assign any value previously to the double, I have no idea why it doesn't "has next"


FINAL EDIT: I don't think I can accept Mouad EL Fakir's answer since a couple of people flagged this a duplicate (which I don't think it is) but it has been answered below:

Most online compilers do not support "stdin" so they provide you with some ways to enter your inputs before running your programs, look at this example here : ideone.com/lFBn2N – Mouad EL Fakir


I'm trying to compile this program that was working just fine in Eclipse yesterday. However, in my free time when I'm out and about I thought I'd practice using online compilers using public computers. So far I've tried 3 different online compilers all giving the same error with different versions of the code.

    import java.util.Scanner;

    class practice{
      public static void main(String args[]){

        Scanner ui = new Scanner(System.in); // ui = "UserInput"
        double i;    
        System.out.println("Please enter your number: ");
        i = ui.nextDouble();
        System.out.print("Your entered number is: ");
        System.out.println(i);
        }
    }

It's a simple enough code AND it compiles. The problem I'm getting is when it executes the compilation:

"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.nextDouble(Scanner.java:2413)
    at practice.main(file.java:9)
Please enter your number:"

What confuses me is that if I comment out

i = ui.nextDouble();

and assign a value to i, it compiles and runs just fine. Which is why I don't understand why the error I'm getting is before "Please enter your number" output. Any help would be appreciated!

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 2
    You first have to use the scanner.hasNext method to check if the scanner has data to read, the problem is that the scanner has no data to read. – SteelToe Feb 03 '17 at 18:51
  • What did you enter as input? – Ayoub Falah Feb 03 '17 at 18:56
  • 2
    Please check [this Google search on your problem](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:stackoverflow.com+java+scanner+nosuchelementexception). You will want to use similar searches of this site in the future. – Hovercraft Full Of Eels Feb 03 '17 at 18:57
  • 2
    Most online compilers do not support "stdin" so they provide you with some ways to enter your inputs before running your programs, look at this example here : https://ideone.com/lFBn2N – Mouad EL Fakir Feb 03 '17 at 19:03
  • 1
    @HovercraftFullOfEels This is not a duplicate question since it's related only to online compilers and running fine on Eclipse. I looked at the thread you linked and it does not solve the issue. – SparrowTail Feb 03 '17 at 19:07
  • @MouadELFakir Cheers mate! I fooled around and saw that option you were talking about! It's running alright now ... it'll be a little awkward using it like that but I'm just practicing beginner stuff anyway. – SparrowTail Feb 03 '17 at 19:08
  • @SparrowTail I edited title of this question hoping it describes problem you are facing better. I also changed duplicate target, take a look at it (especially at EDIT 1 part in accepted answer) and let me know if you agree with it it being a duplicate. – Pshemo Feb 03 '17 at 21:15

0 Answers0