Assume the existence of Java code that already includes thes

Assume the existence of Java code that already includes these two statements: Scanner stdIn - new Scanner (System, in); char entry; Then, implement an input-validation do loop that prompts the user to enter \'y\' or \'n\' and assigns the entered value to entry. If, the entered value is not either \'y\' or \'n\', the code should repeat the prompt and the input assignment an indefinite number of times until the input is either \'y\' or \'n\'.

Solution

import java.util.*;
class validation
{
   public static void main(String ss[])
   {
       Scanner stdIn = new Scanner(System.in);
       char entry;
       do
       {
           System.out.println(\"Enter your choice (y/n) only. Other characters are not allowed\");
           entry = stdIn.nextLine().charAt(0);
           //Validates the entry of y or n
           if(entry == \'y\' || entry == \'n\')
               //If it is y or n then break will take you out of the loop
               break;
       }while(true); //Loops infinite times
   }
}

Output 1:

Enter your choice (y/n) only. Other characters are not allowed
p
Enter your choice (y/n) only. Other characters are not allowed
e
Enter your choice (y/n) only. Other characters are not allowed
u
Enter your choice (y/n) only. Other characters are not allowed
y

Output 2:

Enter your choice (y/n) only. Other characters are not allowed
t
Enter your choice (y/n) only. Other characters are not allowed
s
Enter your choice (y/n) only. Other characters are not allowed
i
Enter your choice (y/n) only. Other characters are not allowed
q
Enter your choice (y/n) only. Other characters are not allowed
n

 Assume the existence of Java code that already includes these two statements: Scanner stdIn - new Scanner (System, in); char entry; Then, implement an input-va

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site