Assume the existence of Java code that already includes thes
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

