KiwiNinja
KiwiNinja

Reputation: 63

String.format not displaying full message

String Quote = String.format(
    "This triangle has an perimeter of %.2f", TP ," and an area of %.2f",TA
);
System.out.printf(Qoute);

the following code prints out "

This triangle has an perimeter of 17.94

" and leaves out the "," and an area of %.2f",TA". What am I doing wrong?

Upvotes: 1

Views: 61

Answers (3)

Atri
Atri

Reputation: 5831

As per the doc:

public static String format(String format, Object... args)

Parameters:
format - A format string
args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored.


So to your question:

" and leaves out the "," and an area of %.2f",TA". What am I doing wrong?

Since the extra arguments are ignored after the 1st arg, it leaves out the "," and an area of %.2f",TA"

Upvotes: 0

You should pass all the parameters at the end

Example:

instead doing this:

String Quote = String.format(
    "This triangle has an perimeter of %.2f", TP ," and an area of %.2f",TA
);

do...

String Quote = String.format("This triangle has an perimeter of %.2f and an area of %.2f", TP, TA);
System.out.printf(Quote);

Upvotes: 3

Suresh Atta
Suresh Atta

Reputation: 122008

You given the wrong format. The later params will considered as inputs to the first param. Try

String quote = String.format(
    "This triangle has an perimeter of %.2f  and an area of %.2f", 
    TP ,TA
);

Upvotes: 6

Related Questions