r/javahelp • u/BrutalBananaW • 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?
8
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
valueOfStringin your enum (becausevalueOfin 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
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
2
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
valueOfto do the mapping as well.
18
u/mk321 Sep 30 '19 edited Sep 30 '19
1. String
As in the answers above you can use
String.But you should use more fitted variable types to your data.
What if in your variable
genderwill be text that you didn't expect (because user type it or you get it in another way)? For example:Your code based on your expectations (only
"male"and"female") won't work properly.2. boolean
The simplest solution is using
boolean(Booleanif you allow unset value):There are only two values that can be set (
true,false, and if needednull).But you have to assume which value correspond to which gender. For example
trueis"male"andfalseis"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:
And use it as new (your) data type:
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).