Results 1 to 16 of 16
Thread: Convert Char To String
- 10-27-2009, 10:04 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 10
- Rep Power
- 0
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!
-
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.
-
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.
- 10-27-2009, 10:57 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 10
- Rep Power
- 0
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.
- 10-27-2009, 11:10 PM #6
Member
- Join Date
- Oct 2009
- Posts
- 10
- Rep Power
- 0
ok sorry.
next time i will notify
-
- 10-27-2009, 11:15 PM #8
Member
- Join Date
- Oct 2009
- Posts
- 10
- Rep Power
- 0
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?
-
Post your exact assignment protocol and any code that they've given you. I'd rather interpret what they say myself.
- 10-27-2009, 11:25 PM #10
Member
- Join Date
- Oct 2009
- Posts
- 10
- Rep Power
- 0
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
-
then scrap your getGender() method and make it a true getter:
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:getGender() { return gender; // since this is already a String }
Java Code:if (sex == 'm') { gender = "...." } else .....
- 10-27-2009, 11:32 PM #12
Member
- Join Date
- Oct 2009
- Posts
- 10
- Rep Power
- 0
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!
-
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.
- 10-27-2009, 11:50 PM #14
Member
- Join Date
- Oct 2009
- Posts
- 10
- Rep Power
- 0
i dont want to talk about them.
again thank you!
- 10-28-2009, 08:39 AM #15
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
-
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
-
Convert Char Array to String Array
By Mayur in forum New To JavaReplies: 8Last Post: 10-12-2009, 11:41 AM -
Convert Comparable object to string or char
By ScKaSx in forum New To JavaReplies: 4Last Post: 01-25-2009, 02:02 PM -
Anyone know how to take info from a txt file and convert it to a char array?
By 2potatocakes in forum New To JavaReplies: 9Last Post: 09-11-2008, 02:51 AM -
char to string
By kian_hong2000 in forum New To JavaReplies: 2Last Post: 08-25-2008, 01:51 PM -
Cannot convert from char to String error
By sondratheloser in forum New To JavaReplies: 1Last Post: 12-13-2007, 09:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks