Feb 02

Now the String is in the right order:

System.out.printf(“%2$+012.3f”,16.7, 18.8);

but what is the output?

Antwort/ answer
Tagged with:
Feb 01

What is the output of the following Code?

System.out.printf(“%f2$+012.3″, 16.7, 18.8);

Antwort/ answer
Tagged with:
Jan 31

Now we have nearly completed all definitions regarding output-formatting for SCJP, only their order is missing:

%<optional:$><optional:+-0,(><optional:placeholder><indicator, eg b,s, i,c,f>

In any case % and indicator need to be given, everything else is optional.

Tagged with:
Jan 30

Depending where you live or for which market you are developing, you might need from time to time to replace ‘.’ with ‘,’ . Therefore you can use ‘;’:

System.out.printf(“%f”, 1234.12345678); // default; with ‘.’ System.out.printf(“\n%,f”, 1234.12345678); // with ‘,’

As an additional benefit, a ‘.’ is added for each 1000. The Output is:


Tagged with:
Jan 29

Methods System.out.printf (); and System.out.format();  also provide possibility to put negative numbers in brackets. ‘+’ as leading is is not possible at the same time and will be ignored:

System.out.printf(“%(d”, 100);
System.out.printf(“\n%(d”, +100);
System.out.printf(“\n%(d”, -100);
System.out.printf(“\n%(+d”, -100);

Output is:


Tagged with:
Jan 28

Since a negative leading sign is always shown, a positive sign is only shown in case ‘+’ is explicitly added:

System.out.printf(“%d”, +100); // Output without leading sign; ‘+’  is without impact in relation to argument
System.out.printf(“\n%d”, -100); // Output with leading sign, since ‘-’ is with impact in relation to argument (in opposite to ‘+’)
System.out.printf(“\n%+d”, 100); // Output with ‘+’
System.out.printf(“\n+%d”, +100); // Output with ‘+’, same as above
System.out.printf(“\n%+d”, -100); // Output with ‘-’, ‘+’ will be ignored

Output is


Wichtig ist, zu wissen, dass das ‘+’-Zeichen nur im Zusammenhang mit ‘+’ in der Formatierung erscheint, das ‘-’ Zeichen immer, unabhängig davon, was in der Formatierung angegeben wurde.

Tagged with:
Jan 27

Usually, everything is right aligned, but it is possible to align a placeholder on the left-side with ‘-’:

System.out.printf(“\n%-12d”, 100);

The output is:


As you can see, no spaces are filled up on the left side as we are used to from previous examples. To make it clearer I am

System.out.printf(“\nstart:%12d:end”,100); // right-alignment (default)
System.out.printf(“\nstart:%-12d:end”,100); // left-alignment

The Output

start:         100:end
start:100         :end

shows clearly that once the place is reserved once in front of and once after the figure. Therefore the figures are either right or left aligned.

Tagged with:
Jan 26

First of all for comparison definition and output without placeholder:

System.out.printf(“%d”, 100);
System.out.printf(“\n%f”, 100.1);

Output is:


I guess there is no need to explain that ‘\n’ is used for line-break :)

Now let’s have a close look on place-holders:

System.out.printf(“\n%12d”, 100); // placeholder for 12 signs of an int-value
System.out.printf(“\n%12.2f”,100.1); // placeholder for alltogether 12 signs (2 signs after the ‘.’ one for the ‘.’ and remaining 9 in front of ‘.’)
System.out.printf(“\n%012.2f”,100.1); // placeholder as above, but filling not-used signs with 0

The formatted output is now:


Quite important to know in this context (and during SCJP-test) is also to know that too small place-holders in front of ‘.’ are automatically expanded:

System.out.printf(“\n%1.2f”, 1234.12345678);

is therefore leading to an output of 1234.12

Tagged with:
Jan 25

As the one or the other knows already from Shell-Scripting with ‘$’ the output-order can be changed. Same applies for Java:

System.out.printf(“%2$d will be first output, %1$d will be second”,100, 200);

In this case the first argument will be in the second output and the second argument the first output. Please keep here (especially during SCJP-Test) in mind, that with arrays there is an argument with position 0.

The Output is

200 will be first output, 100 second

Tagged with:
preload preload preload
http://www.wikio.de Blog Top Liste - by TopBlogs.de Blogverzeichnis - Blog Verzeichnis bloggerei.de Bloggeramt.de Software
Webbhotell Top Blogs