• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with converting char to string and char arrays  RSS feed

 
Camila Leonard
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My teacher gave my class the task of writing a simplified bank savings system in Java. I have to create four classes: agency, savings, dependent and health savings(this one extends from savings).
I'm having a little trouble at two things: on the dependents class we have two private attributes: the name of string type and relationship of char type that should have the values: 'c' for children, 's' for spouse, 'p' for parents and 'o' for others. So the ideal should be creating an array with this values. I have to create two methods, one for getting the relationship (getRelationship) from the person using the program and one called (translateRelationship) that has to return/print the initial of the relationship 's' as ''spouse'' for example. The first method should I just ask the user with Scanner to give me a 'c', 's' or 'p'? (but it wouldn't make sense to even create an array, right?) and the way I did the second method still returns it as a character and not as a string. Anyone could help me with a way of doing this? Thanks for the help.
 
Carey Brown
Saloon Keeper
Posts: 3317
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried so far?

Regarding user input, are you expecting a user to enter 's' or 'spouse'?
 
Camila Leonard
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The teacher wants the user to enter the initial 's' and then we have to create a method to translate it to the word equivalent (string).  I tried like this but it's stupid cause it wont return as a string, it will just print out the phrase. I'm kinf of lost cause we never worked with char types before and he didn't give any examples on the course.


 
Camila Leonard
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I corrected my code:



the program translates the car but it stills shows my relationship as 's' and not "spouse" when I inspect the object;

at the end I have to call the method toString

 
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does the program get a char value entered by a user?  Most methods read Strings from users.
What method and class are you using to get the char value?

Why not read a String from the user?
 
Carey Brown
Saloon Keeper
Posts: 3317
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that when the user enters 's' that you are receiving this as a String, you could modify your method to take a relationshipCode parameter. Something along these lines.

Or, if your code is really a char:
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would consider a switch statement instead of multiple ifs.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or a Map<String,String>
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Camila Leonard wrote:The first method should I just ask the user with Scanner to give me a 'c', 's' or 'p'?

Think, what you'd need to do if user would give you C instead c, or S instead s. With String data type might would be easier for you to work with in such case.

Important: [1] You need to improve your code style. After 'if' always add space before '('. [2] Then, after methods declaration and '(..)', but before '{' you need to add space. [3] No space after the method name/before parameters opening round bracket.



 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to avoid lots of return statements within translateRelationship() method. Would be cleaner if you'd declare a variable, i.e. relationshipStatus and then would return that.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(spouse == 's') {
    return "spouse";
    relationship = "spouse";
}

I'm not sure this one would compile. Assignment statement supposed to be unreachable code. IDE probably would give you some hints on that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!