Results 1 to 16 of 16
  1. #1
    fh84 is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default Convert Char To String

    hello to all,

    public class Person
    {
    protected String name;
    protected String gender;
    protected int dateOfBirth;
    protected String address;
    protected String natInsceNo;
    protected String phoneNo;
    protected static int counter;//class variable


    public Person(String nme,String addr, char sex, int howOld, String ins,String phone)
    {
    dateOfBirth = howOld;
    gender = sex;
    name = nme;
    address = addr;
    natInsceNo = ins;
    phoneNo = phone;
    counter++;
    }

    public String getGender()
    {
    if (gender == 'm' || gender == 'M')
    return "Male";
    else if (gender == 'f' || gender == 'F')
    return "Female";
    else
    return "Wrong Input";
    }



    on this code, the user insert a char and the programs returns a String
    could you please tell how to make the conversion?
    thank you in advanced!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You've already got a method to do the conversion for you, getGender(). I recommend that you change method to allow it to accept a char parameter, and inside of the method, test the parameter's value as you are trying to do with the gender String. Note that the gender String has no business inside of this method since you will use the result returned by the method and put it into the gender String.

    Also, when posting code, please use code tags so that your code is readable. Please see my signature below for more on this.

    Myself, I'd use an enum to represent gender rather than a String.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    By the way. Please do not cross-post questions on different fora without specifying this in your post. Doing this potentially wastes our time creating answers that have already been given, and in general upsets the volunteers here and elsewhere. I'm sure that this is not your goal.

  4. #4
    fh84 is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default

    i didn't post in purpose because i got no answer! so other would waster their time too.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by fh84 View Post
    i didn't post in purpose because i got no answer! so other would waster their time too.
    Please re-read my post. I'm not upset that you cross-posted. I am upset that you cross-posted without notification in your initial thread. There's a big difference. We'll both get over this, just please don't do this again else you risk getting on folks "do not help" list.

    Edit: also, you may wish to reply to my first post here.
    Last edited by Fubarable; 10-27-2009 at 11:07 PM.

  6. #6
    fh84 is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default

    ok sorry.
    next time i will notify

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by fh84 View Post
    ok sorry.
    next time i will notify
    Thanks. Again, you've not responded to my first post. Have you tried this yet?

  8. #8
    fh84 is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default

    well i did try it and worked.
    but this is not what the assignment says.
    the gender variable will only get filled when a new person is created, therefore the getGender() method does not allow an input!

    is there other way to make it?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Post your exact assignment protocol and any code that they've given you. I'd rather interpret what they say myself.

  10. #10
    fh84 is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default

    protected instance variables

    gender // String

    //constructor
    Person(String nme, char sex, Date dob)
    //post instance variables initialised with params
    //address != null, natInscNo != null, counter increment

    //accessor

    getGender()

    hope it helps
    there are some other specs but are not relative

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    then scrap your getGender() method and make it a true getter:
    Java Code:
    getGender() {
      return gender; // since this is already a String
    }
    and that code that you currently have in your getGender method -- put it in your constructor but check the sex parameter with it, and then set the gender var, i.e.,

    Java Code:
    if (sex == 'm') {
      gender = "...."
    }  else .....

  12. #12
    fh84 is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default

    am not sure if i am able to use the getter method.
    ill find out and let you know
    thank you so much for your help!

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    No problem. By the way, it has been my experience that jverd and flounder are among two of the most knowledgeable Java programmers and helpful posters that I know of. I know that you got off to a bad start with them on the other forum, but they are in general quick to forgive and will likely be able to help you much if in future posts you concentrate on the suggestions and don't take any criticisms personally. Though it may not seem like it, the criticisms are there to help you. And no, I'm not either of them. I'm another cussing poster in that forum.

  14. #14
    fh84 is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default

    i dont want to talk about them.
    again thank you!

  15. #15
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    Quote Originally Posted by Fubarable View Post
    SNIP.
    And no, I'm not either of them. I'm another cussing poster in that forum.
    You are? Who, if you don't mind?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    So you cross-posted to a 3rd site without noting it here or elsewhere, and after being requested not to do this. My guess is your goal is to alienate everyone into never helping you here or at other sites. Much luck, you'll need it.

Similar Threads

  1. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  2. Convert Comparable object to string or char
    By ScKaSx in forum New To Java
    Replies: 4
    Last Post: 01-25-2009, 02:02 PM
  3. Replies: 9
    Last Post: 09-11-2008, 02:51 AM
  4. char to string
    By kian_hong2000 in forum New To Java
    Replies: 2
    Last Post: 08-25-2008, 01:51 PM
  5. Cannot convert from char to String error
    By sondratheloser in forum New To Java
    Replies: 1
    Last Post: 12-13-2007, 09:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •