3

I have this simplified code:

System.out.printf("%-*s   Exam Percentage: %4.1f   Final Grade: %c\n", VAR, "fox"+", "+"jim",25.3225, 'C');

Which, according to this and other resources, should produce a column 'VAR' characters wide to the effect of:

 fox, jim   Exam Percentage:  25.3    Final Grade: C

It runs through a for loop and VAR is the largest size of the first string.

I reconize the method:

System.out.printf("%-"+VAR+"s   Exam Percentage: %4.1f   Final Grade: %c\n", "fox"+", "+"jim",25.3225, 'C');

It works, but I am mainly curious. Also, the afore mentioned page mentions 'argsize' an explanation into that may provide some insight.

P.S. I know the the code has literals. Its for simplicity.

2
  • 3
    Sorry, I'm confused. What exactly is your question? :-/ Commented Feb 26, 2013 at 3:43
  • You don't expect System.out.printf is defined as in your Lava resource? System.out is a java.io.PrintStream. Commented Feb 26, 2013 at 3:48

1 Answer 1

1

Format strings for method printf of a java.io.PrintStream such as System.out are defined as Java format strings. They differ from the documentation you linked which is for something called Lava. In particular the width for a percent-conversion in Java is a number and cannot be replaced by an asterisk (*). In other words, as you noticed, it's not OK to write %-*s but it is OK to write %-20s or whatever your preferred number is.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.