validate char

import java.io.*;

public class validate_char
{
      public static void main(String[] args)throws Exception
      {
            //BufferedReader userInput=new BufferedReader(new InputStreamReader(System.in));
            char gender, input;
            boolean valid;
            do{
            System.out.println("Enter Gender: ");
            input = (char)System.in.read();
                  gender=Character.toUpperCase(input);
                  if (gender !='F' || gender !='M')
                  {
                        System.out.print("Please enter M or F only.");

                  }
                  else

            }while (gender !='F' || gender !='M');

            System.out.println(gender);
      }
}
LVL 3
dandeliondreamAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
>>i want the last printIn to display big cap F or M.

gender = gender.toUpperCase();
0
 
dandeliondreamAuthor Commented:
Hi I would like to validate user input. User is allowed to enter F or M only. However, it don't seem to work. Pls advise.
0
 
CEHJCommented:
Your question is ..?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
CEHJCommented:
You need to use the BufferedReader or Scanner
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> if (gender !='F' || gender !='M')

if (gender !='F' && gender !='M')

>> else

else break ;
0
 
dandeliondreamAuthor Commented:
It still doesn't work.

Revised code:
import java.io.*;

public class validate_char
{
      public static void main(String[] args)throws Exception
      {
            //BufferedReader userInput=new BufferedReader(new InputStreamReader(System.in));
            char gender, input;
            boolean valid;
            do{
            System.out.println("Enter Gender: ");
            input = (char)System.in.read();
                  gender=Character.toUpperCase(input);
                  if (gender !='F' && gender !='M')
                        System.out.print("Please enter M or F only.");
                        else break;
            }while (gender !='F' && gender !='M');

            System.out.println(gender);
      }
}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
What is the value of input being read? Try printing it and see:

>> input = (char)System.in.read();
>> gender=Character.toUpperCase(input);

input = (char)System.in.read();
System.out.println ( "Input: " + input ) ;
gender=Character.toUpperCase(input);
System.out.println ( "Gender: " + gender ) ;
0
 
CEHJCommented:
           BufferedReader userInput=new BufferedReader(new InputStreamReader(System.in));
            String gender = null;
            do{
                                          System.out.print("Enter Gender  (Please enter M or F only): ");
                                          gender = userInput.readLine();
            }while ("mMfF".indexOf(gender) < 0);

            System.out.println(gender);
0
 
dandeliondreamAuthor Commented:
Output:Enter Gender:
q
Please enter M or F only. Enter Gender:
Please enter M or F only. Enter Gender:
Please enter M or F only. Enter Gender:
m
M

I only want 1 error message "Please enter M or F only. Enter Gender:" but the program repeats 3 times.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Works on my machine if I enter M.
0
 
Mayank SConnect With a Mentor Associate Director - Product EngineeringCommented:
Oh that is probably due to storage of characters in the input buffer. Use a BufferedReader as CEHJ said and it'll clear it for you.
0
 
dandeliondreamAuthor Commented:
it works if user enters F,f,m or M. However, when i type in other character, the error message is displayed 3 times. i want only 1 error message to display.
0
 
dandeliondreamAuthor Commented:
i want the last printIn to display big cap F or M.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Try with the BufferedReader code
0
 
objectsCommented:
your code is reading the eol markers and thus displaying error message for q, \n and \r
uncomment the BfferedReader line you already have and use its readLine(*) method instead of System.in directly to correctly handle eol

            input = userInput.readLine();
            if (input.length()==0)  // added check for empty input
                System.out.print("Please enter M or F");
            } else {
                gender=Character.toUpperCase(input.charAt(0));
                ...
0
 
dandeliondreamAuthor Commented:
thanks CEHJ and mayankeagle...
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.