Andrea
Andrea

Reputation: 73

Not sure why my code is giving me an "Illegal Format Conversion Exception" error?

I'm pretty sure the error is caused by the last line.. and from what i can tell, it's not pleased that I'm using "%d", variable. But isn't this valid input for an integer?

import java.util.Scanner;

public class total_cost {
    public static void main(String[] args) {

        int TVs;
        int VCRs;
        int controller;
        int CD;
        int recorder;
        double TV_price;
        double VCR_price;
        double controller_price;
        double CD_price;
        double recorder_price;
        double tax;
        {
            TV_price = 400.00;
            VCR_price = 220.00;
            controller_price = 35.20;
            CD_price = 300.00;
            recorder_price = 150.00;
            tax = .0825;

            Scanner in = new Scanner(System.in);
            {
                System.out.printf("How many TV's were sold? ");
                TVs = in.nextInt();

                System.out.printf("How many VCR's were sold? ");
                VCRs = in.nextInt();

                System.out.printf("How many remote controller's were sold? ");
                controller = in.nextInt();

                System.out.printf("How many CD players were sold? ");
                CD = in.nextInt();

                System.out.printf("How many Tape Recorder's were sold? ");
                recorder = in.nextInt();

                System.out.printf("QTY\tDESCRIPTION\tUNIT PRICE\tTOTAL PRICE\n");
                System.out.printf("%d", TVs + "\tTelevision\t%f", TV_price
                        + "\t" + tax * TV_price + "%f", TV_price);
            }
        }
    }
}

Error message:

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
    at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4011)
    at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2725)
    at java.util.Formatter$FormatSpecifier.print(Formatter.java:2677)
    at java.util.Formatter.format(Formatter.java:2449)
    at java.util.Formatter.format(Formatter.java:2383)
    at java.lang.String.format(String.java:2781)
    at total_cost.main(total_cost.java:37)

Upvotes: 1

Views: 12822

Answers (4)

I'm not exactly that familiar with java specifically, but almost all "Print Format" functions that I know follow a pattern with the format string first, and all of the arguments afterwards.

like so

printf("This is a string %d %d %s %f", arg1, arg2, arg3, arg4);

What you seem to be trying to do is to do both format strings and normal concatenation at the same time.

Upvotes: 0

nullByteMe
nullByteMe

Reputation: 6391

You're getting this exception because of this line:

System.out.printf("%d", TVs + "\tTelevision\t%f", TV_price + "\t" + tax * TV_price + "%f", TV_price);

The TVs + "\tTelevision\t%f" is trying to do an arithmetic operation because of the +.

You should format your output and then provide the values.

Example: System.out.printf("%d %s", TVs, "\tTelevision\");

Upvotes: 0

Axel
Axel

Reputation: 14159

You pass a String, not an integer. it should be like this:

System.out.printf("%d\tTelevision\t%.2f\t%.2f,%.2f", TVs, TV_price,tax * TV_price, TV_price);

PS: I took the freedom to format prices with 2 decimal places.

Upvotes: 4

Lev Kuznetsov
Lev Kuznetsov

Reputation: 3728

You should decide if you want to use printf() or println() which seems to be far more suited to you:

System.out.println(3 + "\tTelevision\t" + TV_price + "\t" + tax * TV_price + " " + TV_price);

With printf() you first put the format string in entirety then all the values like this:

System.out.printf("%d number %s string\n", 3, "hi");

Upvotes: 1

Related Questions