Solved

validate char

Posted on 2007-03-26
17
251 Views
Last Modified: 2010-03-31
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);
      }
}
0
Comment
Question by:dandeliondream
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 5
  • +1
17 Comments
 
LVL 3

Author Comment

by:dandeliondream
ID: 18793795
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 18793815
Your question is ..?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18793824
You need to use the BufferedReader or Scanner
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 18793832
>> if (gender !='F' || gender !='M')

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

>> else

else break ;
0
 
LVL 3

Author Comment

by:dandeliondream
ID: 18793878
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 18793930
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 18793972
           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
 
LVL 3

Author Comment

by:dandeliondream
ID: 18793987
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 18794005
Works on my machine if I enter M.
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 50 total points
ID: 18794021
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
 
LVL 3

Author Comment

by:dandeliondream
ID: 18794030
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
 
LVL 3

Author Comment

by:dandeliondream
ID: 18794056
i want the last printIn to display big cap F or M.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18794064
Try with the BufferedReader code
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 18794499
>>i want the last printIn to display big cap F or M.

gender = gender.toUpperCase();
0
 
LVL 92

Expert Comment

by:objects
ID: 18796325
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
 
LVL 3

Author Comment

by:dandeliondream
ID: 18799629
thanks CEHJ and mayankeagle...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18800210
:-)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question