Hello again, I finished my code and it works fine (it's far from elegant), but when I tried to format the printing of a few values, I started getting errors.
Here is the code (errors occur after the loop):
Scanner in = new Scanner (System.in); int w = 0; int d = 0; int p = 0; double total = 0; double overall = 0; int n = 0; double max = 0; double min = 0; String maxTroop = ""; String minTroop = ""; System.out.println("Cookie Sales Report for Young Scouts of America"); System.out.println("Enter the troop name (or exit to quit): "); String t = in.nextLine(); while (!t.equals("exit")) { System.out.println("Enter number of boxes of Chocolate Mint Wafers: "); w = in.nextInt(); in.nextLine(); System.out.println("Enter number of boxes of Peanut Delights: "); d = in.nextInt(); in.nextLine(); System.out.println("Enter number of boxes of Coconut Plops: "); p = in.nextInt(); in.nextLine(); total = w*3+d*3.5+p*2.25; System.out.printf("The total collected by Troop " + t + " is $%7.2f\n", total); overall += total; n++; if (1 == n) { max = total; min = total; maxTroop = t; minTroop = t; } else if (total > max) { max = total; maxTroop = t; } else if (total < min) { min = total; minTroop = t; } System.out.println("Enter the troop name (or exit to quit): "); t = in.nextLine(); } double avg = overall/n; System.out.printf("The total collected overall is $%7.2f\n", overall + "."); if (n > 0) { System.out.printf("The average collected per troop is $%7.2f\n", avg); System.out.printf("The minimum was collected by Troop " + minTroop + " and is $%7.2f\n", min + "."); System.out.printf("The maximum was collected by Troop " + maxTroop + " and is $%7.2f\n", max + "."); } }
I get this error message:
The total collected overall is $Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source) at java.util.Formatter$FormatSpecifier.printFloat(Unknown Source) at java.util.Formatter$FormatSpecifier.print(Unknown Source) at java.util.Formatter.format(Unknown Source) at java.io.PrintStream.format(Unknown Source) at java.io.PrintStream.printf(Unknown Source) at tjf1218_lab5.Scouts.main(Scouts.java:66)
Why is this happening? Does it have something to do with the variable declarations earlier in the program?
Any help would be greatly appreciated. Thanks!


LinkBack URL
About LinkBacks
Reply With Quote
