相关文章推荐
力能扛鼎的筷子  ·  height 100 overflow ...·  2 年前    · 
欢乐的灭火器  ·  BeginEdit,CancelEdit和E ...·  2 年前    · 
健壮的墨镜  ·  博士申请 | ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer

Ask Question

I do not know how to fix my format conversion. Help would be appreciated.

import javax.swing.JOptionPane;
public class SoftwareSales {
public static void main(String[] args) {
// This program displays the amount of discount (if any) and the total amount of the purchase after the discount.
    String userInput;
    String userOutput;
    double packagePrice = 99;
    int userNumberOfPackages;
    double discount = 0;
    double subTotal = 0;
    double total = 0;
    userInput = JOptionPane.showInputDialog( "Enter how many packages you are buying" );
    userNumberOfPackages = Integer.parseInt( userInput );
    if ( userNumberOfPackages < 10){
        subTotal = packagePrice * userNumberOfPackages;
        discount = 0;
        total = subTotal - discount;
    } else if ( userNumberOfPackages < 20 ){
        subTotal = packagePrice * userNumberOfPackages;
        discount = ( (double)20/100) * subTotal;
        total = subTotal - discount;
    } else if ( userNumberOfPackages < 50 ){
        subTotal = packagePrice * userNumberOfPackages;
        discount = ( (double)30/100) * subTotal;
        total = subTotal - discount;
    } else if ( userNumberOfPackages < 100 ){
        subTotal = packagePrice * userNumberOfPackages;
        discount = ( (double)40/100) * subTotal;
        total = subTotal - discount;
    } else{
        subTotal = packagePrice * userNumberOfPackages;
        discount = ( (double)50/100) * subTotal;
        total = subTotal - discount;
    userOutput = String.format( "Packages purchased: \nSubtotal: $\nDiscount: $\nTotal: $", userNumberOfPackages, subTotal,
            discount, total);
    JOptionPane.showMessageDialog( null, userOutput);
    System.exit(0);
                a) Usually there's a line number associated with the error message. Please mark the line that is mentioned. b) Your code can be reduced until you get a minimal reproducible example. Do that for us. We don't like reading dozens of lines of code if the problem can be expressed in 5 lines.
– Thomas Weller
                Jul 18, 2018 at 14:02
                at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)         at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2806)         at java.util.Formatter$FormatSpecifier.print(Formatter.java:2753)         at java.util.Formatter.format(Formatter.java:2520)         at java.util.Formatter.format(Formatter.java:2455)         at java.lang.String.format(String.java:2940)         at SoftwareSales.main(SoftwareSales.java:45)
– user10100043
                Jul 18, 2018 at 14:07

Let's sum up the comments, since they will not only fix your problem but describe the procedure for any problem.

  • Get all the exception information. That is type, message and callstack.
  • Look at the call stack and identify the topmost class you have written yourself.
  • Get the line number.
  • Look at that line and think of potential issues.
  • Applied to your case:

    at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302) at
    java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2806) at
    java.util.Formatter$FormatSpecifier.print(Formatter.java:2753) at
    java.util.Formatter.format(Formatter.java:2520) at
    java.util.Formatter.format(Formatter.java:2455) at
    java.lang.String.format(String.java:2940) at 
    SoftwareSales.main(SoftwareSales.java:45) 
    

    Only SoftwareSales.java was written by yourself. So the problem is in SoftwareSales.java, line 45.

    It's this line:

    userOutput = String.format( "Packages purchased: \nSubtotal: $\nDiscount: $\nTotal: $", userNumberOfPackages, subTotal,
                discount, total);
    

    With String.format() you want to insert 4 numbers into a string. But where should they be inserted? There's no placehoder.

    Looking at some documentation:

    format: a format string

    And a format string needs something with a %. Likely %ffor floating point and %d for decimal.

    Finally (untested):

    userOutput = String.format( "Packages purchased: %d\n
           Subtotal: $%f\n
           Discount: $%f\n
           Total: $%f", 
           userNumberOfPackages, subTotal, discount, total);
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.