inttime[] is an array that already holds single-digit integer values. The first for loop seeks to convert each integer to a binary string and store it to the string array bintime[]. This part works fine, but when I try to run the next for loop, I get an illegal format conversion error.
for(int j = 0; j < inttime.length; j++){
bintime[j] = Integer.toBinaryString(inttime[j]);
}
for(int a = 0; a < bintime.length; a++){
System.out.println(String.format("%04d",bintime[a]));
}
Why is this happening if bintime[] is a String array?
If the
bintimearray holdsStrings– as you’ve stated and you’re assigning to the array fromInteger.toBinaryString– you’ll see an exception fromformattrying to print theStringasint: the%dformat expects anint, and not aString.If you’re trying to pad the
Stringrepresentation of a base 2 number, then use something like:The
4in the format string represents the minimum width of theString, shorter strings will be padded with spaces.