I have this piece of code to validate an input, this input is relative to a bet made by an user
- it needs to be an int
- it needs to be greater than 0
- it needs to be lower than the credits available (the user credits)
- if the user writes a string it should say: input + " is not a valid number"
- if the user writes an int below or equals to zero it should continuously saying: "Please enter a positive number: "
- if the user writes an int higher than the credits available it should say: "not enough credits"
the code I have right now is the following:
Scanner scanner = new Scanner(System.in);
int number;
do {
System.out.print("Please enter a positive number: ");
while (!scanner.hasNextInt()) {
String input = scanner.next();
System.out.printf(input + " is not a valid number.\n");
}
number = scanner.nextInt();
} while (number < 0);
System.out.printf("You have entered the number " + number);
I have the 1. and 2. figure it out with this code, but I can't for nothing add the third (3.) statement as it asks for double the same input or it gives an error InputMismatchException because it suppose to be an integer, but on test sometimes does that.
the number is suppose to be the bet
Please help xD