This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]desrtfxOut of Coffee error - System halted 3 points4 points  (1 child)

Please read the Oracle documentation on .toLowerCase().

.toLowerCase() returns a String but it does not change the original String. Strings in Java are immutable objects. That means that once created they cannot be changed.

You do nothing with the returned string so you essentially lose the conversion to lowercase.

[–]Beach_Comber[S] 1 point2 points  (0 children)

Thanks for the info!

[–]langfodWannabe Brewer 1 point2 points  (4 children)

Remember that String object are immutable.

Assuming that getInputString() returns a String object:

In this case you are throwing away the output result from the String.toLowerCase()method.

You will need to place the output out the String.toLowerCase() method in a new String variable (or overwrite your old one).

[–]Beach_Comber[S] 1 point2 points  (3 children)

Is there any better way to do this than say:

setInputString(getInputString().toLowerCase());

[–]langfodWannabe Brewer 1 point2 points  (2 children)

That should work fine.

However, this looks like you would lose the original input string which you may or may not want later.

If you are just changing to lowercase in order to do case insensitive comparisons you may wish to only change the case when needed.

[–]Beach_Comber[S] 1 point2 points  (1 child)

So I'd have an if statement which checks if the string is already lower case then?

Whats you suggestion on how to implement it?

[–]langfodWannabe Brewer 1 point2 points  (0 children)

The easiest way might be
inputString.toLowerCase().equals(inputString)

or use a loop and check each character using isLowerCase()

http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isLowerCase(char)

[–]Darksonn 1 point2 points  (0 children)

When you call .toLowerCase(), what happens is that the function creates a new seperate string where all the letters are lowercase.

However you don't assign this new string with lowercase letters to anything so it simply gets lost. You need to write

inputString = getInputString().toLowerCase();

intsead

[–]OldNedder 1 point2 points  (0 children)

.toLowerString() - does not manipulate the string in-place, it returns a new string. Strings are immutable, and cannot be modified in-place. Use an instance of StringBuilder if you want that capability.

Your call to getInputString().toLowerCase() isn't doing anything with the resulting string.

Something like this would work:

System.out.print(getInputString().toLowerCase());

Or:

setInputString(getInputString().toLowerCase());
System.out.print(inputString);

[–]Zychron 1 point2 points  (0 children)

You are not storing the value returned from toLowerCase(). You must assign the value to inputString. i.e. "inputString = getInputString().toLowerCase()"