GMX GMX - 1 year ago 143
Java Question

Android studio strange warning

I'm new to code in Android Studio and when i set a integer in a text like this:


This code give me back a warning:

Implicitly using the default locale is a common source of bugs: Use String.format (Locale,...)

What is the correct code for put an integer in a .setText?

I founded more question on stackoverflow but don't apply to this.

Answer Source

What is the correct code for put an integer in a .setText?

You simply need to convert your int as a String, so you can use Integer.toString(int), your code should then be:


If you want to set a fixed value simply use the corresponding String literal, so here your code could simply be:


You get this warning because String.format(String format, Object... args) will use the default locale for your instance of the Java Virtual Machine which could cause behavior change according to the chosen format since you could end up with a format locale dependent.

For example if you simply add a comma in your format to include the grouping characters, the result is now locale dependent as you can see in this example:

System.out.println(String.format(Locale.FRANCE, "10000 for FR is %,d", 10_000));
System.out.println(String.format(Locale.US, "10000 for US is %,d", 10_000));


10000 for FR is 10 000
10000 for US is 10,000
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download