Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.IllegalFormatConversionException: f != java.lang.String Error

import javax.swing.JOptionPane;

public class Minutes {

    public static void main(String[] args) {
        double  BasePlanCost = 20;
        final double BaseCostPerMinute=0.15;

        double MinutesUsed = Double.parseDouble(JOptionPane.showInputDialog("Please enter the amount of minutes Used: "));
        double CostForMinutes = BaseCostPerMinute * MinutesUsed;
        double GrandTotal = BasePlanCost + CostForMinutes;
        JOptionPane.showMessageDialog(null, String.format("$%.2f","**IST Wireless Receipt**","\n","Base Plan Cost:" +BasePlanCost,"/n","Cost For Minutes Used: "+ CostForMinutes,"/n","Grand Total :" +GrandTotal));

    }

}

This program inputs the amount of minutes the user enters and calculates the grand total by adding the CostForMinutes and BasePlanCost. CostForMinutes is calculated by multiplying the minutes the user enters and the BaseCostPerMinute. The out is all the numbers outputted by two decimal places and outputted as a receipt.

When I compile the program it lets me input the amount of minutes but the code collapses and gives me this error

exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String

can anyone help me out?

EDIT this is what I want the output to look like http://i.stack.imgur.com/CubfC.png

like image 835
Jeff Avatar asked Feb 08 '16 20:02

Jeff


1 Answers

You have

String.format("$%.2f","**IST Wireless Receipt**",

This means you want to format the second argument which is a String using %.2f which is a float format which won't work.

You need to re-organize your format to be first and the values you want to format after it.

String.format("**IST Wireless Receipt**%n" +
              "Base Plan Cost: $%.2f%n" +
              "Cost For Minutes Used: $%.2f%n" +
              "Grand Total: $%.2f%n",
              BasePlanCost, CostForMinutes, GrandTotal)
like image 163
Peter Lawrey Avatar answered Nov 05 '22 08:11

Peter Lawrey