The requirement is to input until it is a digit. I created a try-catch within a while loop, the code was supposed to print "Please enter number" before forcing it to print "Enter number", however, the output was reversed. I found out the reason was from the return type, if I change the return type to void, the code will execute properly. Is there any way to keep the return type as float but still print in the right order? Below is the code that has return type as "float"
float inputDigit() {
Scanner sc = new Scanner(System.in);
float digit = 0;
while (true) {
try {
System.out.print("Enter number: ");
digit = Float.parseFloat(sc.nextLine());
return digit;
} catch (NumberFormatException e) {
System.err.println("Please input number");
}
}
}