some logical error in taking up character in java

Posted by Himanshu Aggarwal on Stack Overflow See other posts from Stack Overflow or by Himanshu Aggarwal
Published on 2012-06-28T09:23:04Z Indexed on 2012/06/29 3:16 UTC
Read the original article Hit count: 246

Filed under:
|

This is my code...

class info{
    public static void main (String[]args) throws IOException{
        char gen;

        while(true) {   //problem occurs with this while
            System.out.print("\nENTER YOUR GENDER (M/F) : ");
            gen=(char)System.in.read();

            if(gen=='M' || gen=='F' || gen=='m' || gen=='f'){
                break;
            }
        }

        System.out.println("\nGENDER = "+gen);
    }
}

This is my output...

ENTER YOUR GENDER (M/F) : h

ENTER YOUR GENDER (M/F) :
ENTER YOUR GENDER (M/F) :
ENTER YOUR GENDER (M/F) : m

GENDER = m

Could someone please help me understand why it is asking for the gender so many times.

© Stack Overflow or respective owner

Related posts about java

Related posts about string-comparison