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.

java -How can I validate a single character input. I need to

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!