• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I use char datatype in BlueJ?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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" ?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Patrick Smid
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

EDIT:
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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welkom

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.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char, not CHAR. I have modified your subject for you.
 
Patrick Smid
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic