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!