简体   繁体   中英

Java printf error “Exception in thread ”main“ java.util.UnknownFormatConversionException: Conversion = '4'”

This segment of my code:

    System.out.printf("%f\t\t %.1f %4-s", precipitation[0], temps[0], "Jan.");
    System.out.printf("%f\t\t %.1f %4-s", precipitation[1], temps[1], "Feb.");
    System.out.printf("%f\t\t %.1f %4-s", precipitation[2], temps[2], "Mar.");
    System.out.printf("%f\t\t %.1f %4-s", precipitation[3], temps[3], "Apr.");
    System.out.printf("%f\t\t %.1f %4-s", precipitation[4], temps[4], "May");
    System.out.printf("%f\t\t %.1f %4-s", precipitation[5], temps[5], "Jun.");
    System.out.printf("%f\t\t %.1f %4-s", precipitation[6], temps[6], "Jul.");
    System.out.printf("%f\t\t %.1f %4-s", precipitation[7], temps[7], "Aug.");
    System.out.printf("%f\t\t %.1f %4-s", precipitation[8], temps[8], "Sep.");
    System.out.printf("%f\t\t %.1f %4-s", precipitation[9], temps[9], "Oct.");
    System.out.printf("%f\t\t %.1f %4-s", precipitation[10], temps[10], "Nov.");
    System.out.printf("%f\t\t %.1f %4-s", precipitation[11], temps[11], "Dec.");

Errors with:

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '4' at java.util.Formatter.checkText(Unknown Source) at java.util.Formatter.parse(Unknown Source) at java.util.Formatter.format(Unknown Source) at java.io.PrintStream.format(Unknown Source) at java.io.PrintStream.printf(Unknown Source) at Climate2.main(Climate2.java:71)

Can someone tell me why?

Example values for precipitation[0] and temps[0] would be 5.4f and 51.8f

Change

%4-s

To

%-4.4s 

To truncate all strings to 4 length

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM