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 12 of 12

Thread: Convert CHAR to STRING

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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 getGebder()
    {
    	   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!

    the user has to enter a CHAR and get a STRING
    the error is :Incompatible operand types String and char

    Multiple markers at lines

    line 30 - Incompatible operand types String and char
    line 32 - Incompatible operand types String and char

    this is how the eclipse report the errors!


  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: Convert CHAR to STRING

    You're in luck. Being that a string is of the String class and not a primitive variable, it has it's own methods, one of them being the toString() method. Just input the char as the parameter and it will return a string.

    gender = toString(sex);

    Since gender is a string, and you are trying to give it a char value, you get an error. But when you do what I just wrote, it converts the char sex into a string and fills the value of gender.

    Hope this helps!

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Convert CHAR to STRING

    i did that and the following error :
    The method toString() in the type Object is not applicable for the arguments (char)

  4. #4
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: Convert CHAR to STRING

    Quote Originally Posted by fh84 View Post
    i did that and the following error :
    The method toString() in the type Object is not applicable for the arguments (char)
    Oh I'm sorry, I forgot the class.

    gender = Character.toString(sex);

    Now it should work.
    Last edited by Bill_H; October 27th, 2009 at 09:03 PM.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Convert CHAR to STRING

    Use Java's implicit casting:

    gender = "" + sex;

    I believe you can also explicitly cast to string, but i'm not sure about this one:
    gender = (String) sex;

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Convert CHAR to STRING

    Your problem here is in the declaration of the member called gender.

        protected String gender;

    Change that to be.

        protected char gender;

    // Json

  7. #7
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Convert CHAR to STRING

    no! assignment's specification!

  8. #8
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Convert CHAR to STRING

    hello guys,
    i just tried all the suggested combination and all of them
    came with the following error :
    Incompatible operand types String and char on the following lines

    the error appears both if statements
    public class Person
    {
        protected String name;  
        protected String gender;      //protected attributes are visible in the subclass
        protected int dateOfBirth;
        protected String address;
        protected String natInsceNo;
        protected String phoneNo;
        protected static int counter;//class variable
     
    //Constractor Starts, (returns a new object, may set an object's initial state)
    public Person(String nme,String addr, char sex, int howOld, String ins,String phone)
    {
        dateOfBirth = howOld;
        gender = Character.toString(sex);
        name = nme;
        address = addr;
        natInsceNo = ins;
        phoneNo = phone;
        counter++;
    }    
    //Constractor Ends, (returns a new object, may set an object's initial state)
     
    //Accessor Starts, (Returns info about the state of an object )
     
    public String getGebder()
    {
     
    	   if (gender == 'm' || gender == 'M')
    		   return "Male";
    	   else if (gender == 'f' || gender == 'F')
    		   return "Female";
    	   else 
    		   return "Wrong Input";
    }

  9. #9
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Convert CHAR to STRING

    Ok, in that case, keep the code you have up there using the toString method as such.

    Character.toString(sex);

    And then change your code in the getGebder method to be something like this.

    public String getGebder()
    {
    	   if ("m".equalsIgnoreCase(gender))
    		   return "Male";
    	   else if ("f".equalsIgnoreCase(gender))
    		   return "Female";
    	   else 
    		   return "Wrong Input";
    }

    That should do it.

    // Json

  10. #10
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Convert CHAR to STRING

    it did work thank you so much!!!

  11. #11
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 6 Times in 5 Posts

    Default Re: Convert CHAR to STRING

    would

    String word = "" + sex;

    be considered bad code?

  12. #12
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Convert CHAR to STRING

    It technically compiles and does what you want it too. However, I suppose it's something you should probably avoid.

Similar Threads

  1. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 09:33 AM
  2. Convert Maps of String to Map of Maps
    By abhay8nitt in forum Collections and Generics
    Replies: 1
    Last Post: October 27th, 2009, 07:27 AM
  3. 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
  4. reading a char with SCANNER
    By lotus in forum Java SE APIs
    Replies: 6
    Last Post: July 29th, 2009, 05:03 AM
  5. Program to convert Hexadecimal to its Character equivalent
    By nathanernest in forum Java Theory & Questions
    Replies: 2
    Last Post: April 8th, 2009, 03:12 AM