7

enter image description hereIm using android 4.2 SDK, I have been getting a warning on this line of code:

String text0 = tagalogText.getText().toString();
String textA = text0.substring(0, 1).toUpperCase() + text0.substring(1).toLowerCase();

When I hover over it, it says:

Implicitly using the default locale is a common source of bugs: Use toUpperCase(Locale) instead.

and

Implicitly using the default locale is a common source of bugs: Use toLowerCase(Locale) instead.

I copy the code from Java, not in Java for android. Does anyone know how to remove this error? And why is it now a preferred way to use this method?

Theresa Gamit
  • 292
  • 1
  • 4
  • 14
  • did you checked this answer http://stackoverflow.com/questions/11063102/using-locales-with-javas-tolowercase-and-touppercase and this one http://stackoverflow.com/questions/13444546/android-adt-21-0-warning-implicitly-using-the-default-locale – surhidamatya Oct 30 '13 at 08:02

6 Answers6

5

Just need to clean project.

I had same trouble. I wasn't relying on the default locale, and explicitly tried Locale.US, and Locale.English, yet still got the yellow lint warning. Went away after cleaning.

string.toLowerCase(Locale.ENGLISH);
NameSpace
  • 10,009
  • 3
  • 39
  • 40
2

It's a lint warning, not an error. The reason is that the default locale should be used for user-facing data - if you're rendering something that the user may prefer to see in a style/format specific to their locality.

You need to decide whether it's appropriate to use the default locale:

  • if this is a string that will be used behind the scenes and your architecture expects this string to be uniform across all devices across the world, then you should specify a locale explicitly - usually Locale.US

  • if it's a user-facing string or your architecture doesn't care if it's different on other devices (i.e. it's only used internally, and isn't expected to be in a particular format other than the specified case), then you can safely ignore the lint warning.

ataulm
  • 15,195
  • 7
  • 50
  • 92
1

This is how I solved it:

string.toUpperCase(getResources().getConfiguration().locale));
Droid Chris
  • 3,455
  • 28
  • 31
0

Try using

text0.substring(0, 1).toUpperCase(Locale.ENGLISH);

From doc here

A common mistake is to implicitly use the default locale when producing output meant to be machine-readable. This tends to work on the developer's test devices (especially because so many developers use en_US), but fails when run on a device whose user is in a more complex locale.

For example, if you're formatting integers some locales will use non-ASCII decimal digits. As another example, if you're formatting floating-point numbers some locales will use ',' as the decimal point and '.' for digit grouping. That's correct for human-readable output, but likely to cause problems if presented to another computer (parseDouble(String) can't parse such a number, for example). You should also be wary of the toLowerCase() and toUpperCase() overloads that don't take a Locale: in Turkey, for example, the characters 'i' and 'I' won't be converted to 'I' and 'i'. This is the correct behavior for Turkish text (such as user input), but inappropriate for, say, HTTP headers.

stinepike
  • 54,068
  • 14
  • 92
  • 112
0

It means there can be language problems with relying on the default Locale, which varies between systems. Use this instead:

text0.substring(0, 1).toUpperCase(Locale.US);
Lesleh
  • 1,665
  • 15
  • 24
0

You didn't indicate that you got an answer, so try this (something similar worked for me):

text0 = text0.substring(0, 1).toUpperCase();
text1 = text0.substring(1).toLowerCase();
String textA = text0 + text1;

It's seems as though the assignment is critical.

Let me know if it works.

Take care.

Doug
  • 36
  • 4
  • Hi i tried to use your code but it still didnt work to me. can i have ung fb account so i can contact you about this.. – Theresa Gamit Jul 08 '13 at 21:43
  • I'm so sorry for not getting back to you sooner. Actually, I just saw this since I don't log in regularly. I don't have a fb account. but I'm surprise the assignment didn't work. If the message you see is just a warning, I'd ignore it and move on. Take care. – Doug Nov 08 '13 at 01:42