Kay
Kay

Reputation: 1

Trouble using printf %f %n with a double, then adding a string?

I'm trying to make it so that a list of numbers, with 6 dedicated characters, and two 2 decimal figures appears, followed by a "|".

for (int c = 1; c < Config.MAX_VALUE; c++)
        System.out.printf("%6.2f %n",(double)c + "|");

I'm having trouble adding the "|" and get the error

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

How might I format it properly?

Upvotes: 0

Views: 55

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198093

Move the | character to the format string, not to the format arguments. It looks like you want

 System.out.printf("%6.2f| %n",(double)c);

%f only knows how to format number types, and you're passing it a String from converting c to a double, then to a String, and then adding a | to the end. That gives you a String, which %f doesn't know how to format.

Upvotes: 5

Related Questions