Formatting api doesn't have any specific conversion for array types.
But For String array its is displaying 0th element for int array it will throw exception. It should exception for String array also.
The following code and console will explain the abouve context.
<code>
import java.util.*;
public class Test9 {
public static void main(String[] arg) {
int[] intArray = new int[10];
for(int i=0; i < intArray.length; i++) {
intArray[i]=i;
}
String[] messages = new String[intArray.length];
for(int i=0; i < intArray.length; i++) {
messages[i] = Integer.toString(i);
}
Formatter formatter = new Formatter(System.out);
formatter.format("%s Messagess \n ",messages);
formatter.format("%d",intArray);
}
}
</code>
<console>
vishalb:/home/vv145429/tiger/src/formating/bugs 71 % javac -source 1.5 Test9.java
vishalb:/home/vv145429/tiger/src/formating/bugs 72 % java Test9
0 Messagess
Exception in thread "main" java.util.IllegalFormatConversionException: d != [I
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3639)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2429)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2382)
at java.util.Formatter.format(Formatter.java:2165)
at java.util.Formatter.format(Formatter.java:2100)
at Test9.main(Test9.java:14)
vishalb:/home/vv145429/tiger/src/formating/bugs 73 %
</console>
But For String array its is displaying 0th element for int array it will throw exception. It should exception for String array also.
The following code and console will explain the abouve context.
<code>
import java.util.*;
public class Test9 {
public static void main(String[] arg) {
int[] intArray = new int[10];
for(int i=0; i < intArray.length; i++) {
intArray[i]=i;
}
String[] messages = new String[intArray.length];
for(int i=0; i < intArray.length; i++) {
messages[i] = Integer.toString(i);
}
Formatter formatter = new Formatter(System.out);
formatter.format("%s Messagess \n ",messages);
formatter.format("%d",intArray);
}
}
</code>
<console>
vishalb:/home/vv145429/tiger/src/formating/bugs 71 % javac -source 1.5 Test9.java
vishalb:/home/vv145429/tiger/src/formating/bugs 72 % java Test9
0 Messagess
Exception in thread "main" java.util.IllegalFormatConversionException: d != [I
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3639)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2429)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2382)
at java.util.Formatter.format(Formatter.java:2165)
at java.util.Formatter.format(Formatter.java:2100)
at Test9.main(Test9.java:14)
vishalb:/home/vv145429/tiger/src/formating/bugs 73 %
</console>
- duplicates
-
JDK-5024518 need warning if varargs argument isn't boxed
- Resolved