my code compiles but when i try to run it, an error message says there is an exception at main java.util.illegalFormatConversionException. how can i fix this? i am sure i got the placeholders right for each variable?
import javax.swing.JOptionPane;
public class BookTest
{
public static void main(String args[])
{
double charge;
double totalCharge=0;
double totalTax=0;
double grandTotal=0;
String dataArray[][] = {{"NonFiction", "Abraham Lincoln Vampire Hunter","Grahame-Smith","978-0446563079","13.99","Haper","NY","US","Political"},
{"NonFiction", "Frankenstein","Shelley","978-0486282114","7.99","Pearson", "TX","England", "Historical"},
{"Fiction", "Dracula","Stoker","978-0486411095","5.99","Double Day", "CA","4918362"},
{"NonFiction", "Curse of the Wolfman"," Hageman","B00381AKHG","10.59","Harper", "NY","Transylvania","Historical"},
{"Fiction", "The Mummy","Rice","978-0345369949","7.99","Nelson","GA","3879158"}};
Book bookArray[] = new Book[dataArray.length];
int quantityArray[] = {12, 3, 7, 23, 5};
for (int i = 0; i < dataArray.length; i++)
{
if (dataArray[i][0].equals("NonFiction"))
{
bookArray[i] = new NonFictionBook(dataArray[i][1], dataArray[i][2], dataArray[i][3], Double.parseDouble(dataArray[i][4]),
new Publisher(dataArray[i][5], dataArray[i][6]), dataArray[i][7], dataArray[i][8]);
}
else
{
bookArray[i] = new FictionBook(dataArray[i][1], dataArray[i][2], dataArray[i][3], Double.parseDouble(dataArray[i][4]),
new Publisher(dataArray[i][5], dataArray[i][6]), Integer.parseInt(dataArray[i][7]));
}
}
String msg = "";
for (int i = 0; i < bookArray.length; i++)
{
charge = bookArray[i].calculateTotal(quantityArray[i]);
totalTax = bookArray[i].calculateTax(bookArray[i].calculateTotal(quantityArray[i]));
totalCharge = charge + totalCharge;
grandTotal = totalCharge + totalTax;
if (bookArray[i] instanceof FictionBook)
{
FictionBook fb = (FictionBook)bookArray[i];
msg += String.format("%s %d $%.2f $%.2f\n", fb.getTitle(), fb.getCode(), charge, fb.calculateTax(fb.calculateTotal(quantityArray[i])));
}
if (bookArray[i] instanceof NonFictionBook)
{
NonFictionBook nfb = (NonFictionBook)bookArray[i];
msg += String.format("%s %s $%.2f $%.2f\n", nfb.getTitle(), nfb.getCategory(), charge, nfb.calculateTax(nfb.calculateTotal(quantityArray[i])));
}
}
// ––– this is where the error is at
msg += String.format("Total $%.2f", "Total Tax $%.2f", "Grand Total $%.2f", totalCharge, totalTax, grandTotal);
JOptionPane.showMessageDialog(null, msg);
}
}
Look at this call - I've put the arguments on different lines:
String.format(
"Total $%.2f",
"Total Tax $%.2f",
"Grand Total $%.2f",
totalCharge,
totalTax,
grandTotal)
That's passing "Total $%.2f"
as the format string, and then five values to be formatted within that string - the first of which is itself a string, but you're trying to format it as a float. I suspect you really wanted something like:
String.format(
"Total $%.2f%nTotal Tax $%.2f%nGrand Total $%.2f",
totalCharge,
totalTax,
grandTotal)
Here we've got a format string with three placeholders, and then three values to format - which makes a lot more sense. The %n
within the format string represents a new line.
If that isn't what you wanted, you should be clearer about what you expected the broken code to do...
See more on this question at Stackoverflow