0

I want to create a method that is meant to protect against entering a wrong value for gender.

The method fineGender below, will return true if the correct gender is entered and false otherwise.

// postcondition:  fineGender returns true if argument g equals M, m, F or f 
// and returns false otherwise.
    public static boolean fineGender(char g)
6
Contributors
6
Replies
32
Views
4 Years
Discussion Span
Last Post by andreas.bjorn
0

hint: use simple if else or case conditions for the method or use regEx if you want to deviate from the norm a bit :)

0

an easier implementation would be to make a:

isMale()

method, that returns true if the gender == m or M, false otherwise, that way you don't have to validate the input, neither.

0

Use regular expression and check for these values rather than using if-else:-

Pattern pattern = Pattern.compile("[MmFf]");
Matcher matcher = pattern.matcher(input);

Learn more about regular expression.

2

bit of overkill when we're just trying to compare a single char to four possible values, isn't it?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.