I have the following code:
private static final String PATTERN = "file_%d.txt";
int no; // 1-3
String filename = String.format(PATTERN, no ,Locale.US);
and later on I get an exception saying that
java.io.FileNotFoundException: file_٣.txt
which indicates that %d got replaced with an arabic number. How can that be if I explicitely specify Locale.US?
The %d specifies that the single variable is a decimal integer. The %n is a platform-independent newline character. The output is: The value of i is: 461012. The printf and format methods are overloaded.
%s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string.
By using %n in your format string, you tell Java to use the value returned by System. getProperty("line. separator") , which is the line separator for the current system.
The locale needs to be the first parameter:
String.format(Locale.US,PATTERN, no);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With