Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Char to String - Gender method.

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Char to String - Gender method.

    Hi, I am doing an assignment where I need to get the user to enter m or f in gender and return male.

    My assignment criteria requires me to have gender as a char and sex as a char. I get error after error and can't do anything. Not a very good programmer lol.

    here is my code:
     
    public class Person
    {
        // instance variables
        protected String name;
        protected char gender;
        protected Date dateOfBirth;
        protected String address;
        protected String natInsceNo;
     
        private static int counter;
     
        public static int count()
        {
            return counter;
        }
     
        /**
         * Constructor for objects of class Person
         */
        public Person(String nme, char sex, Date dob, String insceNumber)
        {
            // initialise instance variables
            name = nme;
            dateOfBirth = dob;
            gender = sex.charAt(0); 
            natInsceNo = insceNumber;
            counter = 0;
        }
     
        public Person(Person other)
        {
            counter++;
        }
     
        public void getGender(char sex)
        {
            if ("m".equalsIgnoreCase(gender))
               return "Male";
            else if ("f".equalsIgnorecCase(gender))
               return "Female";
            else
               return "Unknown Gender";
        }
     
        public void copy(Person other)
        {
            //creates another instance of Person
            name = other.name;
            gender = other.gender;
            dateOfBirth = other.dateOfBirth;
            natInsceNo = other.natInsceNo;
        }    
     
        public boolean equals(Person other)
        {
            //returns true if the name and national insurance number are the same as the values in other.
            return name.equals(other.name) && natInsceNo.equals(other.natInsceNo);
        }
    }
    Last edited by copeg; November 22nd, 2010 at 09:57 AM. Reason: Please use the code tags.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Char to String - Gender method.

    For future reference, please flank your code with the [highlight=java][/highlight] tags, and please post any and all errors. For starters, your getGender method is attempting to return a value when the definition of the return value is void.

  3. The Following User Says Thank You to copeg For This Useful Post:

    ibby50 (November 23rd, 2010)

  4. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Char to String - Gender method.

    What errors are you getting?

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    ibby50 (November 23rd, 2010)

  6. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Char to String - Gender method.

    Never mind guys, I figured it out. Turns out I had set Gender twice and turned out I didnt need the m or f if statement. Thanks anyways.

  7. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Char to String - Gender method.

    I'm having exactly the same problem. How did you fix it?

  8. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Char to String - Gender method.

    Please don't resurrect a thread that is two years old. Rather, please start your own thread and include all the necessary details.

Similar Threads

  1. Getting a string value from within a method in another class
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 11th, 2010, 04:17 PM
  2. passing string into method
    By tabutcher in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 08:43 AM
  3. string method
    By dstha in forum Object Oriented Programming
    Replies: 3
    Last Post: February 25th, 2010, 08:07 PM
  4. Convert CHAR to STRING
    By fh84 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 29th, 2009, 09:21 PM
  5. Need help with concatenizing char? to string?
    By bh-chobo in forum Java SE APIs
    Replies: 3
    Last Post: October 16th, 2009, 08:40 AM