public static int intInput(String prompt, String error) {
int intInput = 0; // Variable to be returned
// Start the scanner
Scanner keyboard = new Scanner(System.in);
// This loop check to make sure the user entered positive data
do {
System.out.println(prompt);
// This loop check if the user has entered an int
while (!keyboard.hasNextInt()) {
System.out.println(error);
keyboard.next();
}
intInput = keyboard.nextInt();
} while (intInput <= 0);
// Returns an int
return intInput;
}
If the user presses enter without entering any data, the prompt won't show back up. It just goes to the next line and it won't give any indication that the user can still enter data.
I changed next to nextLine, but I still get the same error
public static int intInput(String prompt, String error) {
int intInput = 0; // Variable to be returned
// Start the scanner
Scanner keyboard = new Scanner(System.in);
// This loop check to make sure the user entered positive data
do {
System.out.println(prompt);
// This loop check if the user has entered an int
while (!keyboard.hasNextInt()) {
System.out.println(error);
keyboard.nextLine();
}
intInput = keyboard.nextInt();
} while (intInput <= 0);
// Returns an int
return intInput;
}
Okay, so I read through the link and tried the using the nextLine deal, but it still didn't change anything
// Method for user int input
public static int intInput(String prompt, String error) {
int intInput = 0; // Variable to be returned
// Start the scanner
Scanner keyboard = new Scanner(System.in);
// This loop check to make sure the user entered positive data
do {
System.out.println(prompt);
// This loop check if the user has entered an int
while (!(keyboard.hasNextInt())) {
System.out.println(error);
keyboard.nextLine();
}
intInput = keyboard.nextInt();
keyboard.nextLine();
} while (intInput <= 0);
// Returns an int
return intInput;
}