• Post Reply Bookmark Topic Watch Topic
  • New Topic

Case conversion problem  RSS feed

 
Dominic Griffin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote the code in an attempt to capitalize the irst letter of each word in a sentence, but it only capitalizes the first word in the first sentence. Does anyone see where I went wrong?

 
Vijitha Kumara
Bartender
Posts: 4002
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have other problem than converting to uppercase here.
Change,


to


to use a one delimeter,

or use a RegulerExpression to use more than one.


 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember .? are "metacharacters" in regular expressions, and the text you pass to String#split is a regular expression. So .? doesn't mean stop/dot/period or question mark. Have a look at the link you have been given, and this Java™ Tutorials section.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't you split at whitespace as well when you want to split around every word?

Also, why are you removing the last character of your words?
The end index is exclusive, you the last included character is that at position begin.length() - 2.

Also, substring creates a new String object. I would use a character instead for the first character:
Now there is still a possibility for error if you encounter the empty string though; you should either check for it or ensure it doesn't occur by taking into account multiple occurrences of punctuation characters:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!