I have to build a user input template where you can enter a number between 100 and 200. If you type "goodbye", it needs to exit, if you enter a letter, symbol, number outside the range, etc. it needs to prompt you to try again. if you enter a number within the range, it will move on to another question (I haven't included that in the code below).
I got almost everything to work. If you enter a correct number the first time, it works. The "goodbye" and wrong character functions also work. BUT if you enter the wrong character (number or letter) and then, when it prompts you to try again, you put a CORRECT number in the range, it does not accept it and again prompts you to try again.
I need to figure out how to input a wrong character (such as a letter or number outside of range), and then when it prompts you to try again and you put a number between 100-200, it works and exits the loop (which will enable it to go on to the next question/loop).
Here is my code:
Scanner console = new Scanner(System.in);
String Input;
int Number;
boolean isValid = true;
do {
System.out.println("Enter a valid number:");
Input = console.nextLine();
if (Input.equals("goodbye") || Input.equals("GOODBYE")){
System.out.println("You have left");
System.exit(0);
}
char[] numberTester = Input.toCharArray();
for (int i = 0; i<Input.length(); i++){
Character currentChar = new Character (numberTester[i]);
if (!currentChar.isDigit(numberTester[i])){
isValid = false;
System.out.println("Invalid. Try again.");
i = Input.length();
}
if (isValid == true){
Number = Integer.parseInt(Input);
if (Number < 100 || Number > 200){
isValid = false;
System.out.println("Invalid. Try again.");
}
}
}
} while (isValid == false);
}
}
forloop altogether.