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

后端 未结 2 635
北恋
北恋 2021-01-18 19:07
import javax.swing.JOptionPane;

public class Minutes {

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


        
相关标签:
2条回答
  • 2021-01-18 19:15

    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

    http://www.oracle.com/technetwork/java/codeconventions-135099.html

    0 讨论(0)
  • 2021-01-18 19:37

    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)
    
    0 讨论(0)
提交回复
热议问题