java - Changing the format specifier in the printf() method results in IllegalFormatFlagsException -
in following code, after changing format specifier in printf()
method %04d%n
%0-4d%n
, , running code results in
exception in thread "main" java.util.illegalformatflagsexception:
flags = '-0'
the complete source code follows:
public class minarray { public static void main(string[] args) { byte[] storeminimum = new byte[5]; byte[] trialarray = new byte[15]; for(byte bt=0; bt < storeminimum.length; bt++){ randomize(trialarray); storeminimum[bt] = findminimum(trialarray); } (byte minvalue : storeminimum) system.out.printf("%0-4d%n", minvalue); } private static byte findminimum(byte[] valarray) { byte minvalue = valarray[0]; for(byte bt=0; bt < valarray.length; bt++) minvalue = (byte) math.min(minvalue, valarray[bt]); return minvalue; } private static void randomize(byte[] valarray) { (byte bt = 0; bt < valarray.length; bt++) valarray[bt] = (byte) (math.random()*128); } }
can explain, how have happened, since wanted byte literal displayed left-justified 4 character positions? also, please suggest fix same.
i think when write
system.out.printf("%04d%n", minvalue);
it's right-justified. empty space in left replaced '0'.
the '-' used left-justified.
they can't used together.
Comments
Post a Comment