Solved

validate char

Posted on 2007-03-26
17
241 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
  • 6
  • 5
  • 5
  • +1
17 Comments
 
LVL 3

Author Comment

by:dandeliondream
Comment Utility
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
Comment Utility
Your question is ..?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You need to use the BufferedReader or Scanner
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> if (gender !='F' || gender !='M')

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

>> else

else break ;
0
 
LVL 3

Author Comment

by:dandeliondream
Comment Utility
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:mayankeagle
Comment Utility
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
Comment Utility
           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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Works on my machine if I enter M.
0
 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
i want the last printIn to display big cap F or M.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Try with the BufferedReader code
0
 
LVL 86

Accepted Solution

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

gender = gender.toUpperCase();
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
thanks CEHJ and mayankeagle...
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 42
network + 7 73
advertisement module in core php 4 79
mockito example issue 8 29
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now