Question

How to combine following System.out.format statements into 1, so that it prints:

1a. Longest string: 'sentence', Size: '8'

My code:

    System.out.format("1a. Longest string: '%s'",longestString);  
    System.out.format(", Size: '%s'%n",tokens.length);

Thank you.

Was it helpful?

Solution 2

This is simple:

  • Combine the format strings,
  • Combine the argument list.

The format method goes through the format string, and plugs in the elements of the additional parameter list in places where it sees a format specifier. That is why combining the two is a purely mechanical task.

OTHER TIPS

If I understand your question, you could do it like this -

System.out.format("1a. Longest string: '%s', Size: '%s'%n",
    longestString, tokens.length);  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top