Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try this:</p> <pre><code>System.out.printf( "The length of the line between the points (%d, %d) and (%d, %d) is %f%n", x1, x2, y1, y2, distance); </code></pre> <p>The simplest solution for these cases is to use a formatted string, as shown in the code above. See more details of the syntax of such as string in the <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html" rel="nofollow noreferrer">documentation</a>.</p> <p>In the above snippet, each character after a <code>%</code> indicates that the corresponding parameter in that position (after the string, in left-to-right order) must be formatted accordingly. In particular:</p> <ul> <li>%d : This is going to be an integer. The first four parameters are the <code>int</code>s <code>x1, y1, x2, y2</code></li> <li>%f : This is going to be a decimal number. The fifth parameter is the <code>double</code> called <code>distance</code></li> <li>%n : This is going to be a platform-specific new line character</li> </ul> <p>The <a href="http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#printf%28java.lang.String,%20java.lang.Object...%29" rel="nofollow noreferrer"><code>printf</code></a> method takes care of replacing each format character with the corresponding parameter value, creating and printing a <code>String</code> as expected. This is much easier and less error-prone than concatenating the string parts with the <code>+</code> operator, interspersing the required variables.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload