Solved

validate char

Posted on 2007-03-26
17
250 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate jars 4 61
How to convert from xls to xlsx using java 7 95
dao vs facade design patterns 2 38
Strange router problem - can't access hotmail.com 14 55
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…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

730 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