Home Ask Login Register

Developers Planet

Your answer is one click away!

Jeff February 2016

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


Peter Lawrey February 2016

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)

Jesus Zavarce February 2016

Try to organize your message as:

String message = String.format(
            "**IST Wireless Receipt** \n" + 
            " Base Plan Cost:$ %.2f \n" +
            " Cost For Minutes Used: $ %.2f \n" +
            " Grand Total : $ %.2f", BasePlanCost, CostForMinutes, GrandTotal);

    JOptionPane.showMessageDialog(null, message);

I recommended you to read the code conventions of the java language


Post Status

Asked in February 2016
Viewed 3,758 times
Voted 10
Answered 2 times


Leave an answer

Quote of the day: live life