Why is printf producing an compiler error?

Friday, July 11, 2014

I'm trying to make program that shows the time of day and fraction of day that has passed which must use printf. I can't figure out why printf is producing this compiler error:


Exception in thread "main" java.util.DuplicateFormatFlagsException: Flags = ' '



at java.util.Formatter$Flags.parse(Formatter.java:4140)

at java.util.Formatter$FormatSpecifier.flags(Formatter.java:2555)

at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2625)

at java.util.Formatter.parse(Formatter.java:2480)

at java.util.Formatter.format(Formatter.java:2414)

at java.io.PrintStream.format(PrintStream.java:920)

at java.io.PrintStream.printf(PrintStream.java:821)

at FractionOfDay.fractionOfDay(FractionOfDay.java:22)

at FractionOfDay.main(FractionOfDay.java:26)

public class FractionOfDay
{
public static void fractionOfDay(int hour, int minute, int second, char ampm)
{
double fractionofday = hour/12 + minute/720 + second/43200 + ampm/2;
if (ampm == 'A')
{
String ampmoutput = "AM";
}
else
{
String ampmoutput = "PM";
}
if (hour < 10)
{
String space = " ";
}
else
{
String space = " ";
}
System.out.printf("%2d:%2ds%%s %4f", hour,minute,"space","ampmoutput",fractionofday);
}
public static void main(String[] args)
{
fractionOfDay(7,15,0,'P');
}
}






http://ift.tt/1mpDD3y