• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use the capitalize() method correctly  RSS feed

 
Stephen Corrad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I'm new in this forum so please bear with me if I'm posting this topic on the wrong thread!

I'm trying to create a private method called capitalize() which takes String nameModel in any uppercase/lowercase combination and returns it with the first letter uppercased and all other lowercased. E.g. "stePHeN" returns "Stephen" . This is what I've written so far:


Unfortunately it doesn't work and it still returning me the String modelName in its original uppercase/lowercase combination. Is there anyone that could give me some advice on this?

Thanks in advance
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Your code should work. Maybe the mistake is in how you are calling your method. Can you show how you are calling it?

There's just one error in the method, but that shouldn't affect the result in most cases. The error is in this line:

The == operator does not do what you think it does with strings (or other objects). It does not check if modelName is equal to the string "" - what it does is check if the variable modelName refers to the exact same String object as the String object for "". It's a subtle but important difference.

you should use .equals() instead of == to check the content of strings:

Another small error: your method won't work for strings that contain exactly 1 character - it will throw an exception for strings that contain 1 character.
 
Stephen Corrad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jasper,

first of all thanks for taking the time to reply to my question. This is how I'm calling the method:

public Car(String newModelName)
{
setModelName(newModelName);
}
/**
* @return The model name of the car
*/
public String getModelName(){
return modelName;
}

private String makePrettyString(String modelName) {
if (modelName ==null) {
return null;
} if (modelName =="") {
return "";
} else{
return modelName.substring(0,1).toUpperCase() + modelName.substring(1).toLowerCase();
}
}

Does it make sense?

Thanks again.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see that you are calling your method makePrettyString anywhere in your code.
 
Stephen Corrad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess that's exactly where I'm stuck. Where should I call the makePrettyString method in my code?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know, that depends on what you want to use it for. Maybe in the constructor of your class Car?

 
Stephen Corrad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, it works now!

Thank you very much for your help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!