hi
whats the meaning of these signs?
for example:
System.out.printf("%6s %6s %10s", "x1", "x2", "(y1, y2) \n");
hi
whats the meaning of these signs?
for example:
System.out.printf("%6s %6s %10s", "x1", "x2", "(y1, y2) \n");
In printf, the pattern %6s %6s %10s declares three columns, each expecting a string (%s) printed with a minimum field width of 6, 6, and 10 characters. If a value is shorter than its width, it is padded with spaces on the left (right-aligned by default). If it is longer, it is not truncated; the whole value is printed and the column expands. The literal spaces between the specifiers in your format string produce actual spaces between the columns. Note that %s formats any object via String.valueOf(arg), so null becomes "null" and objects use their toString().
A few practical tips for tabular output:
%n in the format string for a platform-independent newline instead of embedding \n inside an argument. Put the newline at the end of the format, not inside a data value.- to left-align within the width (e.g., %-10s). This is helpful for headings and neat columns.printf will throw an IllegalFormatConversionException or MissingFormatArgumentException.Example:
System.out.printf("%-6s | %-10s%n", "Col1", "Col2");
System.out.printf("%-6s | %-10s%n", "A", "Hello"); See the full format syntax in the Formatter spec at Oracle Javadoc and PrintStream.printf details at PrintStream.printf.
Jump to Post— Xeros606 0They format output. s formats strings, d formats integers, and f formats double precision floating point data (double). Basically you can use them to trim a string down so it only displays a set amount of characters ("%.2s", for two characters only as an example), rounds to a certain amount …
They format output. s formats strings, d formats integers, and f formats double precision floating point data (double). Basically you can use them to trim a string down so it only displays a set amount of characters ("%.2s", for two characters only as an example), rounds to a certain amount of decimal places ("%.5f" for 5 decimal places - if unspecified, it defaults to 6 decimal places), or to add commas to a large integer to make it easier to read ("%,d"). There are more things worth knowing about formatted output, google it and check out this web page.
They format output. s formats strings, d formats integers, and f formats double precision floating point data (double). Basically you can use them to trim a string down so it only displays a set amount of characters ("%.2s", for two characters only as an example), rounds to a certain amount of decimal places ("%.5f" for 5 decimal places - if unspecified, it defaults to 6 decimal places), or to add commas to a large integer to make it easier to read ("%,d"). There are more things worth knowing about formatted output, google it and check out this web page.
thanks :)
System.out.printf("%6s %6s %12s", "x1", "x2", "(y1, y2) \n"); output:
(6space) x1 (6space) x2 (6space) (y1, y2)
System.out.printf("%6s %6s %12s", "x1", "x2", "(y1, y2) \n");output:
(6space) x1 (6space) x2 (6space) (y1, y2)
ThankQ, very clear :icon_cheesygrin:
what does java mean
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.