I've been messing around trying to figure out why I'm getting this compiler error for a few hours, and I'm just not sorting it out. Any help would be appreciated.
Thanks,
David
Code:
package dladoucer_week5;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner( System.in );
int cont = 0;
double tax = .00346;
int oldReading;
int newReading;
int usage;
double rate = 0;
double subtotal;
double taxes;
double total;
while (cont == 0) {
System.out.println("Welcome to the City Power Bill Calculator!");
System.out.println("Please enter your previous meter reading:");
oldReading = input.nextInt();
System.out.println("Please enter your current meter reading:");
newReading = input.nextInt();
usage = newReading - oldReading;
if (usage <= 500) {
rate = .0809;
subtotal = rate * usage;
taxes = subtotal * tax;
total = subtotal + taxes;
System.out.printf("Your usage was: %d KwHs\n",usage);
System.out.printf("Your rate is: $ %5.4d/KwH\n",rate);
System.out.printf("Your Subtotal will be: $%.2d\n",subtotal);
System.out.printf("Your taxes are: %.2d\n",taxes);
System.out.printf("Your total bill this month is: %.2d\n",total);
System.out.println("\nCalculate a new usage? "
+ "(-1 to Exit, 0 to Continue)");
cont = input.nextInt();}
else if (usage >= 501 && usage <= 900) {
rate = .091;
subtotal = rate * usage;
taxes = subtotal * tax;
total = subtotal + taxes;
System.out.printf("Your usage was: %d KwHs\n",usage);
System.out.printf("Your rate is: $ %5.4d/KwH\n",rate);
System.out.printf("Your Subtotal will be: $%.2d\n",subtotal);
System.out.printf("Your taxes are: %.2d\n",taxes);
System.out.printf("Your total bill this month is: %.2d\n",total);
System.out.println("\nCalculate a new usage? "
+ "(-1 to Exit, 0 to Continue)");
cont = input.nextInt();}
else if (usage > 901) {
rate = .109;
subtotal = rate * usage;
taxes = subtotal * tax;
total = subtotal + taxes;
System.out.printf("Your usage was: %d KwHs\n",usage);
System.out.printf("Your rate is: $ %5.4d/KwH\n",rate);
System.out.printf("Your Subtotal will be: $%.2d\n",subtotal);
System.out.printf("Your taxes are: %.2d\n",taxes);
System.out.printf("Your total bill this month is: %.2d\n",total);
System.out.println("\nCalculate a new usage? "
+ "(-1 to Exit, 0 to Continue)");
cont = input.nextInt();}
}
}
}
When I run it I get:
run:
Welcome to the City Power Bill Calculator!
Please enter your previous meter reading:
750
Please enter your current meter reading:
1235
Your usage was: 485 KwHs
Exception in thread "main" java.util.IllegalFormatPrecisionException: 4
at java.util.Formatter$FormatSpecifier.checkInteger(Formatter.java:2892)
at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2643)
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 dladoucer_week5.Main.main(Main.java:38)
Java Result: 1
BUILD SUCCESSFUL (total time: 9 seconds)