Question: java -How can I validate a single character input. I need to make a loop that will keep asking for a valid single character input
java -How can I validate a single character input.
I need to make a loop that will keep asking for a valid single character input until it gets f or b
here is my (wrong) output.

char alphabetDirection; Scanner s = new Scanner(System.in); do { alphabetDirection = s.next().charAt(0); System.out.println("Invalid. You must enter either 'f' or 'b' to start."); System.out.println("Forwards or Backwards (f/b)? : "); } while (alphabetDirection != 'f' && alphabetDirection != 'b'); }
d Invalid. You must enter either 'f' or 'b' to start. Forwards or Backwards (f/b)? : f f Invalid. You must enter either 'f' or 'b' to start. Forwards or Backwards (f/b)? : For 'f' and 'b' it shouldn't say "invalid...." like it does here. Process finished with exit code 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
