Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

validate char

Posted on 2007-03-26
17
Medium Priority
?
254 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 200 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 800 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

610 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