Hey guys, I'm working on a tip calculator as an excercise from the book I'm using to learn Java, and I've run in to a bit of a problem. When I run this code:
import java.util.Scanner;
public class TipCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print
("Enter the subtotal of your ticket:");
double subtotal = input.nextDouble();
System.out.print
("Enter the percentage you would like to tip:");
double tip = input.nextDouble();
double tipAmount = (subtotal * tip) / 100.0;
double total = (tipAmount + subtotal);
System.out.printf("The tip amount is $" + "%5.2f", tipAmount + "\n" +
"The Total ticket price is $" + "%5.2f", total);
}
}
the program runs normally, until it gets to the final line ("The tip amount is $"...), at which point, the console prints out:
The tip amount is $java.util.IllegalFormatConversionException: f != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4045)
at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2761)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2708)
at java.util.Formatter.format(Formatter.java:2488)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at TipCalculator.main(TipCalculator.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
I'm not sure what's causing this to happen. I'm still learning how to use the printf function, but as far as I know, I've declared it exactly as the example in the book states as the correct usage. Anybody know where I went wrong here? Thanks for any and all help.