I'm using a function where i only want to receive one letter from the user, E or D. I'm using answer = Stdin.readChar(); However, if the user enters more than one character, it has to ask again (evidently). But when it does the loop and fails to exit since the first letter is neither of the two, it simply assesses the next letter in the string that the user entered.
How would I clear this buffer at the end of the loop so that it waits for input? I thought of simply using readLine(), but in this case if it was only one letter and not E or D it pauses to get the new line.
privatestaticvoid readSystemInput()
{
System.out.print("Hello! What is your name?");
boolean first = true;
try
{
while (true)
{
if (!first)
System.out.println("Sorry U entered wrong input enter either E or D");
String name = new BufferedReader(new InputStreamReader(System.in)).readLine();
if (name!=null && name.length()==1)
{
if (name.charAt(0)=='E'||name.charAt(0)=='D')
{
//process ur stuff
System.out.println("Name"+name);
char [] chars = newchar[1];
chars[0] = name.charAt(0);
System.out.println("Char "+new String(chars));
break;
}
}
first = false;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}