System.out.println("_______________");
System.out.println("Enter your deposit amount: ");
System.out.println("_______________");
while (!scnr.hasNextDouble()) {
System.out.println("_______________");
System.out.println("Error: Invalid input. Please try again. 4");
System.out.println("_______________");
scnr.nextLine();
}
double deposit = scnr.nextDouble();
balance = deposit + balance;
System.out.println("_______________");
System.out.printf("Your current balance is $%.2f %n", balance);
I want to loop that error message until the user inputs a double. However, when you get the first invalid input, it prints the error message twice. Any subsequent invalid inputs print the error message only once. I managed to fix this by turning scnr.nextLine();
into scnr.next();
.
Can anyone explain to my why that works but the first one doesn't? Why does scnr.nextLine(); only print twice for the first time?
I want to use nextLine because I don't want the input "g g g g" to print four times like it would with next. However if I hit enter a few times before an invalid input, nextLine prints the error message once for each empty string. IS there a way around this?
This is for a simple ATM simulator I had to do about two months ago for an introductory Java class.