I'm attempting to ask the user for a double value, using Scanner.nextDouble(); but I'm getting a NoSuchElementException before I have a chance to type anything. From what I can see in the documentation, this means that Scanner is at the end of the input. However, I don't understand how that can happen when the Scanner is reading from System.in, not a file.
Is it possible that having other Scanners open is causing this? I don't think that should be the case, as another method is successfully run before this code that opens and closes another Scanner. But I don't really know enough about the ins and outs of Scanner (or much else!) to know if that is the issue.
Here's where I'm at currently, after a lot of failed attempts to re-write this code to get it working:
public static double collectDepositAmount(int accountNum){
System.out.print("Enter how much you would like to deposit in Account #" + accountNum + ": $");
Scanner userInput = new Scanner(System.in);
double finalDeposit;
try{
double inputDeposit;
inputDeposit = userInput.nextDouble();
There's more after that, but the exception always gets thrown at userInput.nextDouble();
. The catch
block is only looking for InputMismatchException, and I close userInput
right before I return a valid double.