• Post Reply Bookmark Topic Watch Topic
  • New Topic

Capitalize the second word of a string  RSS feed

 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so the exercise i'm working on says to have the user enter their name and the program will output their name with the last name in all caps. i made it work BUT the only way i could figure it out was to ask for the first and last names separately creating two strings rather than one.

of course i WANT to make it work how it's supposed to (with one string) so that i'm learning. i'm just having trouble conceptualizing how exactly (with varying lengths of names) to tell the program to only capitalize the second word... at first I thought create a substring beginning with the first letter of the last name and ending with the last...but again, therein lies the issue of varying name lengths.

is there a way to create a substring that beginIndex's at the first "space"? then i could just leave the endIndex parameter empty and it would take the whole word into a new string. and from there utilize toUpperCase to the new string?

here's my code asking for the first and last names separately. thanks in advance for any help!

 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
solved.

 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks pretty good. I always have trouble understanding the parameters of String.substring() and I'm prone to off-by-one errors if I don't think hard, but apart from that the code looks fine. Remember to test it using stupid things like entering a name with no spaces, and just hitting Enter without returning anything.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You absolutely must have each person enter their first and last name separately. There are plenty of last names with spaces in them.

Example: "St. Pierre"

I am very close to a person with one such last name and know for a fact that faulty programming has wreaked havoc for them. The "St." part of their name was dropped from several databases causing registration and purchasing information mismatches. It came to the point where that person basically had to just drop the space and period in their name in all important documents merely to appease these faulty programs. One unfortunate side effect of this is that would people would pronounce the first part of their name as "stuh" instead of "saint".

 
Campbell Ritchie
Marshal
Posts: 56586
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than trying to change Strings like that, try using a StringBuilder.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote:Example: "St. Pierre"

Or "Van den Bosch".

However, I don't agree that you absolutely must ask for them separately. David's solution will work just fine unless the first name has more than one part.

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:David's solution will work just fine unless the first name has more than one part.

Like Billy Bob Thornton?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Winston Gutkowski wrote:David's solution will work just fine unless the first name has more than one part.

Like Billy Bob Thornton?

Always seems to me like that should be hyphenated.

Winston
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words... homework is easy, real life is hard. And homework doesn't necessarily prepare you for real life, as Tyler has shown.

By the way did you know that there are people with only one name? (Not two names like everybody posting to this thread.) Happens in Indonesia. And that there are people whose surname comes first? Billions of them, that's how they do it in China.


 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lot of Brazilians use just one name. Koreans seem to use three but I've seen hyphens used a lot. Its pretty rare but I know I've seen four or more names used. I think its an open problem as to what the best way of entering a name into a database is, but I'm pretty against just entering a full name and letting a program parse it. Maybe just have one "identifying" name, I wouldn't even call it "last", and have the user enter either 0 or 1 strings which may contain multiple names before and after.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow i didn't even think of that - people with two word first or last names. i think for now i shall skip the clusterF of a headache until it may come up later in the text
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't sweat it, homework assignments for beginners are usually made as simple as possible, so as to provide useful training. Just don't confuse them with real life, and when you get to programming real-life work, don't be surprised when real-life situations like Tyler's friend come up and bite you.

If you want to see just one of the issues which might arise if you tried to develop a fully internationalized database containing people's names, have a look at Spanish naming customs. And then if that didn't make your brain hurt, have a look at Portuguese name. Just the first sentence of the latter article warns you of the tar pit ahead!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!