Hello, I am currently working on an assignment for JAVA. I am completely new to JAVA, but I have experience with PHP. Part of the assignment is to be able to make objects where you can store the gender with the datatype CHAR. But I have no idea how. I currently have the following code for this:
The "private char geslacht" is what this is about. I assume you should be able to enter M for male and F for female and then you should display the full word with an if statement. But when I compile it with no errors and then try to create an object with any single character in the "geslacht" field it gives me the error: "Error: Cannot find symbol - variable X". But when I enter a number it works fine. I cant find any usefull tutorial or solution to this problem. Must I in some way declare the characters M and F for "geslacht" ?
How do you do that? Do you prompt the user for input; if you do can we see that code? Which line of code is the one that gives you an error?
The quieter you are, the more you are able to hear.
Joined: Nov 26, 2012
Well, I don't type any code when making an object. BlueJ has some screens where you can just fill in some fields and it creates an object for you.
I am trying it with the codepad now, but that doesnt work. when I copy+paste the correct code you gave, no object is being created. All I get is: " <object reference> (persoon)" with some kind of red block to the left of it. No error or anything.
And for all the code I have:
This is all the code that I am using/have written.
I have nothing for interaction with the user, but that isn't necessary since this is the first assignment for JAVA.
I tried compiling the code you supplied and had no compiler errors or "cannot find symbol."
You are confused about the char (not “CHAR”) datatype. It is not a letter, but a number. So 1=male 2=female makes sense, but is not consistent with 'm'/'f'. And remember that '1' is not equal to 1. A char is not a good way to store data like M/F, but an enumerated type would be ideal. You can even create an enumerated type which returns "man"/"vrouw" from its toString() method.
As an alternative you could use a switch-case statement to initialise geslacht to a String from'm'/'f', but you would have to change the type of geslacht.
You also seem to be confused about passing information to methods. You are passing a String to the setDatum method, and not using that String. In fact you are creating another String in the method, which you never use. That method does nothing at all, if the date of birth was correctly entered previously.
You also have a stub for a setGeslacht(String) method, but the geslacht field is not a String. that will cause confusion and possible compiler errors.
I have broken the long lines in your code. You are using /**...*/ comments in the wrong locations; they should be immediately before a field, method or class. I think you wanted /*...*/ comments.
Thanks for the answers. So I understand a char can only be a number. But a 1 is not a 1? I don't really understand that. That means I can't for example do:
But I have to do it with a char, that's the assignment.
And this whole thing with get and set methods, I have spend hours on this but it doesn't get any clearer. Is there some clear tutorial about it explaining it in detail? I couldn't get any of the tutorials I found to work.
So I have this set and get method for gender:
But it always returns null, what am I doing wrong here?
Joined: Oct 13, 2005
I already told you, 'm' is not equal to 1. Nor is 'v' equal to 2. You can read the actual values (in hexadecimal) here.
Hi Patrick, welcome to the Ranch. You have to read carefully what others have told so far as you are new.
I feel that you are letting the user enter 1 or 2 for "geslacht" field or are passing the values from somewhere within the application.
So see, anything entered through console is String by default. You have to convert that to character (a single digit or letter or symbol).
Say, if I am entering through console <1> or <2> for gender, which means male or female respectively, so I will do a check:
The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Joined: Oct 13, 2005
It might be simpler to ask for gender as "M" "male" "V" "vrouw" etc. I might use a switch-case. Going through the methods of the String class, you should be able to find some methods which can reduce those and similar Strings to 'm', 'v'. You can even use the first letters of "Heer", "Dame", "Female", etc in the same switch statement.