r/javahelp • • Sep 30 '19

Quick beginner question: let’s say I scan the users answer to “what is your gender?” A) Male B) Female How do I store A as “male”?

I want to follow up with a question: Gender: Male Is this correct?

23 Upvotes

17 comments sorted by

18

u/mk321 Sep 30 '19 edited Sep 30 '19

1. String

As in the answers above you can use String.

String gender = "male";

But you should use more fitted variable types to your data.

What if in your variable gender will be text that you didn't expect (because user type it or you get it in another way)? For example:

String gender = "green";

Your code based on your expectations (only "male" and "female") won't work properly.

2. boolean

The simplest solution is using boolean (Boolean if you allow unset value):

boolean gender = true;

There are only two values that can be set (true, false, and if needed null).

But you have to assume which value correspond to which gender. For example true is "male" and false is "female". You have to remember it and use in whole program (and changing assumption is hard). If you will make mistake, you program won't work properly.

3. enum

Better solution is using enum.

You can create enum like this:

enum Gender {
    MALE,
    FEMALE
}

And use it as new (your) data type:

Gender gender = Gender.MALE;

I think this is the best solution. You don't need remember which value in code correspond to which value in real word (because you can name it). You make less mistakes in your code (with this IDE can watch what you write). You can't assign to variable value that not expected. Also you can add more values than two (for example new gender if you want - nowadays there are such requirements for software).

3

u/PM_ME_YOUR_VIOLIN Oct 01 '19

I would definitely agree with the enum solution, but if you do end up with using a boolean a workaround is calling it something like isMale or isFemale as to not be confusing. Not ideal though; use an enum.

Best way might be to use a ternary:

Gender gender = response.equals("A") ? Gender.Male : Gender.Female;

Then...

String genderMessage = gender == Gender.Male ? "Male" : "Female"

8

u/totoro27 Sep 30 '19

I would use a String to store what they type in

3

u/holaredditors Sep 30 '19

Use a string to store it.

Example: Take a string gender to store the gender

Scanner sc=new Scanner(System.in);

String gender;

Sys.out.println("Enter your gender");

gender=sc.nextLine();

0

u/DerekB52 Sep 30 '19

gender is easily represented as a boolean, boolean isMale = true, or false.

However, I'd recommend using an enum.

enum Gender { MALE, FEMALE }

Enums are more extendable, allowing you to add another gender later, much easier than if you had used a boolean.

So, then you'd take input as a string and do something like this

String input = "Male"

if (input.equals("Male") {

gender = Gender.MALE;

} else if (input.equals("Female") {

gender = Gender.FEMALE;

}

Also, it'd be wise to do a string comparison that ignores caps.

1

u/mk321 Sep 30 '19

You can create enum with values (Strings):

enum Gender {
    MALE("male"),
    FEMALE("female");
    
    private final String value;
 
    private Gender(String value) {
        this.value = value;
    }
}

And check it in this way:

if (input.equals(Gender.MALE.value()) {
    gender = Gender.MALE;
} else if (input.equals.FEMALE.value()) {
    gender = Gender.FEMALE;
}

Or you can write your own method valueOfString in your enum (because valueOf in Java based on enum name: Gender.valueOf("MALE") so you lose the advantage of values in enums and have to uppercase input):

public Gender valueOfString(String gender) {
    for (Gender g : values()) {
        if (g.value.equals(label)) {
            return g;
        }
    }
    return null;
}

And you can use it simply:

gender = Gender.valueOfString(input);

-1

u/ThatRandomGamerYT Sep 30 '19

you could tell the user to type in the gender. save it as a string use that to reference.

or you could just let them type in a/b/c/whatever the new genders are and store the alphabets as char and use that as a key

1

u/randomfloridaman Sep 30 '19

You don't want to give the user the ability to enter "cat" as a gender. Need to maintain control over your app by providing a list for the user to select from.

0

u/Manitcor Sep 30 '19

I would enum this one and not store the string. This will give the flexibility to easily add more and change how various types are displayed without a bunch of string handling code.

0

u/[deleted] Sep 30 '19

[deleted]

-1

u/BrutalBananaW Sep 30 '19

Thank you for replying. But how do I ask: “Is “male” correct?” Rather than: “Is “A” correct”

0

u/strghst Sep 30 '19

You can create an enumeration that will store responses as either 0 or 1. Then, if the response is A, store it as 0, which will return male from that enumeration, or female if it's 1.

Using enumerations in this case is the way to go due to it being fixed variables, aka use it if you don't want to implement interesting 21st century stuff like cisgenders.

0

u/NIHKLAS Sep 30 '19

Thats a good idea but I think a bit overkill for the purpose of his little project. In his position I would make a very simple String variable that stores either "male" or "female". That's the easiest way and it does the job. Enums are great for bigger projects where you have a lot of global constants but not just for a simple "either... or..." - question.

0

u/strghst Sep 30 '19

I do agree with you on this one, but I do believe it is good practice to make these things operate well from the start.

It is a better idea to educate professionals who follow guidelines well, and as the person is still learning - it is best to give him advice that will be useful and complementary in the future.

-1

u/evils_twin Sep 30 '19

IF statement

2

u/[deleted] Sep 30 '19

[removed] — view removed comment

1

u/nerdyhandle Sep 30 '19 edited Sep 30 '19

Map this to the Enum mentioned in another comment and this would work quite well.

Also, your answer is exactly how I interpreted OPs question. Everyone seems to have interpreted it differently.

You could aslo override Enum valueOf to do the mapping as well.