File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes How do I use char datatype in BlueJ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I use char datatype in BlueJ?" Watch "How do I use char datatype in BlueJ?" New topic

How do I use char datatype in BlueJ?

Patrick Smid

Joined: Nov 26, 2012
Posts: 3
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" ?
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

I don't see the code which is causing those error messages -- you really should have posted that, since without it we aren't sure what you are talking about.

But my guess is that you did this:

rather than this:

Am I on the right track? If not, then post the code and the error message.
Kemal Sokolovic

Joined: Jun 19, 2010
Posts: 825

Welcome to the Ranch!

But when I enter a number it works fine.

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.
Patrick Smid

Joined: Nov 26, 2012
Posts: 3
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.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46367

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.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

char, not CHAR. I have modified your subject for you.

[Asking smart questions] [About Bear] [Books by Bear]
Patrick Smid

Joined: Nov 26, 2012
Posts: 3
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?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46367
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.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 187

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 |
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46367
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.
I agree. Here's the link:
subject: How do I use char datatype in BlueJ?
It's not a secret anymore!