1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
<html>
<head>
<title>
Handbuch der Java-Programmierung, 5. Auflage
</title>
</head>
<body>
<a name="startofbody"></a>
<script language="JavaScript" src="hjp4lib.js">
</script>
<script language="JavaScript">
installKbdHandler("97,#startofbody;101,#endofbody;116,cover.html;122,k100003.html;115,search.html;105,index.html;100,JDKDOCS;112,APIDOCS;104,k100070.html;106,k100075.html;107,k100077.html;108,k100078.html");
</script>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100070.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100075.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100077.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100078.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">Kapitel 11 - Strings
</table>
<hr>
<!-- Section -->
<a name="ausgabeformatierung"></a>
<h2>11.6 <a name="ixa100702">Ausgabeformatierung</a><a name="ixa100703"></a></h2>
<hr>
<ul>
<li><a href="k100076.html#ausgabeformatierung">11.6 Ausgabeformatierung</a>
<ul>
<li><a href="k100076.html#sectlevel3id011006001">11.6.1 Grundlagen</a>
<li><a href="k100076.html#formatter">11.6.2 Die Formatangaben</a>
</ul>
</ul>
<hr>
<!-- Section -->
<a name="sectlevel3id011006001"></a>
<h3>11.6.1 Grundlagen </h3>
<p>
C-Programmierer vermissen in Java schon länger eine bequeme und
flexible Möglichkeit, elementare Datentypen formatiert auszugeben.
Während Methoden wie <a href="index_t.html#ixb100327"><font color=#000080><tt>toString</tt></font></a>
oder <a href="index_p.html#ixb100555"><font color=#000080><tt>println</tt></font></a>
zwar bequem in der Anwendung sind, bieten sie kaum Möglichkeiten,
die Formatierung der Ausgabe gezielt zu beeinflussen. Und die seit
dem JDK 1.2 vorhandenen Klassen im Paket <a name="ixa100704"><a href="index_j.html#ixb100556"><font color=#000080><tt>java.text</tt></font></a></a>
sind zwar flexibel und mächtig, aber oft umständlich in
der Anwendung und nicht ganz einfach zu verstehen.
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Seit der J2SE 5.0 gibt es die Klasse <a name="ixa100705"><a href="index_j.html#ixb100403"><font color=#000080><tt>java.util.Formatter</tt></font></a></a>,
deren Möglichkeiten der <font color="#000077"><tt>printf</tt></font>-Funktion
in C und C++ ebenbürtig sind. Sie bietet die Möglichkeit,
alle primitiven Datentypen und ihre Wrapper, sowie Datums-/Zeitwerte
der Klassen <a href="index_c.html#ixb100557"><font color=#000080><tt>Calendar</tt></font></a>
und <a href="index_d.html#ixb100558"><font color=#000080><tt>Date</tt></font></a>
in vielfältiger Weise (auch länderspezifisch) formatiert
auszugeben. Prinzipiell muss dazu ein <a name="ixa100706"><a href="index_f.html#ixb100559"><font color=#000080><tt>Formatter</tt></font></a></a>-Objekt
instanziert und dessen Ausgabemethode <a name="ixa100707"><a href="index_f.html#ixb100560"><font color=#000080><tt>format</tt></font></a></a>
aufgerufen werden. Einfacher wird es dadurch, dass <a href="index_f.html#ixb100560"><font color=#000080><tt>format</tt></font></a>
mit all seinen Möglichkeiten auch in den Klassen <a name="ixa100708"><a href="index_s.html#ixb100117"><font color=#000080><tt>String</tt></font></a></a>
und <a href="index_p.html#ixb100114"><font color=#000080><tt>PrintStream</tt></font></a>
zur Verfügung steht. Letztere besitzt als Remineszenz an C/C++
sogar eine Methode <a name="ixa100709"><a href="index_p.html#ixb100165"><font color=#000080><tt>printf</tt></font></a></a>
mit gleicher Funktionalität. Wir wollen uns auf die Klassen <a href="index_s.html#ixb100117"><font color=#000080><tt>String</tt></font></a>
und das Objekt <a name="ixa100710"><a href="index_s.html#ixb100167"><font color=#000080><tt>System.out</tt></font></a></a>
konzentrieren, die übrigen Stellen funktionieren in der gleichen
Weise.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#FF9900">
<tr>
<td><font color="#FFFFFF"> JDK1.1-6.0 </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
</tr>
</table>
<p>
Die Methode <font color="#000077"><tt>format</tt></font> gibt es in
zwei Varianten:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
static String format(Locale l, String format, Object... args)
static String format(String format, Object... args)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/util/Formatter.html" onClick="this.href=getApiDoc('java.util.Formatter')"><font color="#660066" size=-1>java.util.Formatter</font></a></td>
</tr>
</table>
<p>
Bei der ersten Form kann explizit angegeben werden, welche Sprach-
bzw. Ländereinstellung verwendet werden soll, bei der zweiten
wird die Default-<a href="index_l.html#ixb100561"><font color=#000080><tt>Locale</tt></font></a>
verwendet. Der Parameter <font color="#000077"><tt>format</tt></font>
dient zur Übergabe eines Formatstrings, die übrigen Parameter
sind dessen Argumente. Der Formatstring besteht aus Formatangaben
und festem Text. Die Formatangaben beginnen mit einem Prozentzeichen
und enden mit einem Konvertierungszeichen. Dazwischen können
weitere Optionen enthalten sein. Insgesamt sieht die Syntax so aus:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
% [Argument-Index$][Flags][Width][.Precision]Conversion
</pre>
</font>
</td>
</tr>
</table>
<p>
Der Formatstring wird von links nach rechts ausgegeben. Zu jeder Formatangabe
muss es einen Parameter geben, dessen Wert entsprechend formatiert
ausgegeben wird. Alle übrigen Teile des Formatstrings werden
als fester Text angesehen und unverändert ausgegeben. Das folgende
Listing zeigt ein einfaches Beispiel:
<a name="listingid011006"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Listing1106.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing1106
<font color="#555555">004 </font>{
<font color="#555555">005 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">006 </font> {
<font color="#555555">007 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 5; i < 199; i += 37) {
<font color="#555555">008 </font> System.out.format(<font color="#0000FF">"Aktueller Wert: %3d%n"</font>, i);
<font color="#555555">009 </font> }
<font color="#555555">010 </font> }
<font color="#555555">011 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing1106.java"><font color="#000055" size=-1>Listing1106.java</font></a></td>
</tr>
</table>
<i>
Listing 11.6: Formatierte Zahlenausgabe</i></p>
<p>
Die Ausgabe der Methode ist:
<font color="#333300">
<pre>
Aktueller Wert: 5
Aktueller Wert: 42
Aktueller Wert: 79
Aktueller Wert: 116
Aktueller Wert: 153
Aktueller Wert: 190
</pre>
</font>
<p>
An <font color="#000077"><tt>format</tt></font> werden zwei Parameter
übergeben: der Formatstring »Aktueller Wert: %3d%n«
und die Schleifenvariable <font color="#000077"><tt>i</tt></font>.
Der Formatstring enthält den konstanten Text »Aktueller
Wert: «, der unverändert ausgegeben wird, sowie die beiden
Formatangaben »%3d« und »%n«. Die erste gibt an,
dass das nächste Argument als Ganzzahl mit einer Feldbreite von
drei Stellen ausgegeben werden soll. Die zweite steht als Platzhalter
für eine Zeilenschaltung.
<!-- Section -->
<a name="formatter"></a>
<h3>11.6.2 Die Formatangaben </h3>
<p>
Auf diese Weise können auch sehr komplexe Ausgabeformatierungen
erzeugt werden. Dazu wollen wir uns die Bestandteile einer Formatangabe
etwas genauer ansehen.
<ul>
<li>»Conversion« gibt den Datentyp an. Die wichtigsten Möglichkeiten
sind:
<ul>
<li>b - Boolescher Wert
<li>c - Einzelnes Zeichen
<li>d - Ganzzahl in Dezimaldarstellung
<li>o - Ganzzahl in Oktaldarstellung
<li>x - Ganzzahl in Hexadezimaldarstellung
<li>X - Dito, mit großen Buchstaben
<li>f - Fließkommazahl
<li>e - Fließkommazahl mit Exponent
<li>E - Dito, mit großem »E«
<li>g - Fließkommazahl in gemischter Schreibweise
<li>G - Dito, ggfs. mit großem »E«
<li>t - Prefix für Datums-/Zeitangaben
<li>s - Strings und andere Objekte
</ul>
<p>
Anders als in C/C++ müssen die Argumente strikt typkonform übergeben
werden, was mitunter etwas lästig sein kann. Das Konvertierungszeichen
»f« akzeptiert beispielsweise nur Fließkommazahlen,
nicht aber Ganzzahlen; bei »d« ist es genau umgekehrt.
<p>
Einen Sonderstatus haben »%« und »n«, die für
das Prozentzeichen bzw. die Zeilenschaltung stehen und kein zusätzliches
Argument benötigen.
<p>
Sollen mit Hilfe des Konvertierungsprefixes »t« Datums-/Zeitwerte
ausgegeben werden, so muss mit einem zweiten Buchstaben direkt dahinter
angegeben werden, welcher Teil des <a href="index_d.html#ixb100558"><font color=#000080><tt>Date</tt></font></a>-
oder <a href="index_c.html#ixb100557"><font color=#000080><tt>Calendar</tt></font></a>-Objektes
ausgegeben werden soll. Einige der vielen Möglichkeiten sind:
<ul>
<li>H - Stunde, zweistellig, im 24-Stunden-Format
<li>M - Minute, zweistellig
<li>S - Sekunde, zweistellig
<li>d - Tag, zweistellig
<li>m - Monat, zweistellig, 1..12
<li>Y - Jahr, vierstellig
<li>F - Datum, formatiert nach ISO 8601 (YYYY-MM-DD)
<li>c - Kompletter Datums-/Zeitstring inkl. Zeitzone
</ul>
<p>
Mit dem Konvertierungszeichen »s« können Strings, aber
auch andere Objekte ausgegeben werden. Falls das Objekt das Interface
<a name="ixa100711"><a href="index_j.html#ixb100562"><font color=#000080><tt>java.util.Formattable</tt></font></a></a>
implementiert, wird dessen Methode <a name="ixa100712"><a href="index_f.html#ixb100563"><font color=#000080><tt>formatTo</tt></font></a></a>
aufgerufen, andernfalls die (stets vorhandene) Methode <a name="ixa100713"><a href="index_t.html#ixb100327"><font color=#000080><tt>toString</tt></font></a></a>.
<li>Mit »Argument-Index$« kann angegeben werden, auf welchen
Parameter sich die Formatangabe beziehen soll. »1$« steht
dabei für das erste Argument nach dem Formatstring, »2$«
für das zweite usw. Fehlt diese Angabe, werden die Argumente
der Reihe nach zugeordnet, d.h. die erste Formatangabe im Formatstring
bezieht sich auf das erste Argument, die zweite auf die zweite, usw.
Zusätzlich kann hier ein »<« angegeben werden, um
anzuzeigen, dass dasselbe Argument verwendet werden soll wie bei der
vorigen Formatangabe. In diesem Fall darf das Dollarzeichen allerdings
nicht angehängt werden.
<li>Mit »Width« wird die Mindest-Feldbreite angegeben. Benötigt
der auszugebende Wert weniger Zeichen, wird mit Leerzeichen aufgefüllt.
<li>Der Teil ».Precision« gibt bei Fließkommazahlen
die Anzahl der Stellen nach dem Komma an.
<li>Mit den »Flags« können weitere Ausgabeoptionen
abgerufen werden. Nicht alle Flags sind auf alle Datentypen anwendbar.
Die wichtigsten von ihnen sind:
<ul>
<li>- - Linksbündige Ausgabe
<li>+ - Vorzeichen immer ausgegeben
<li>0 - Zahlen werden mit Nullen aufgefüllt
<li>, - Zahlen werden mit Tausenderpunkten ausgegeben
<li>( - Negative Zahlen werden in Klammern eingeschlossen
</ul>
</ul>
<p>
Weitere Details, Möglichkeiten und Einschränkungen werden
in der sehr ausführlichen API-Dokumentation der Klasse <a href="index_j.html#ixb100403"><font color=#000080><tt>java.util.Formatter</tt></font></a>
beschrieben. Das folgende Listing zeigt noch einmal alle besprochenen
Möglichkeiten im Überblick:
<a name="listingid011007"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Listing1107.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.util.*;
<font color="#555555">004 </font>
<font color="#555555">005 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing1107
<font color="#555555">006 </font>{
<font color="#555555">007 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">008 </font> {
<font color="#555555">009 </font> <font color="#00AA00">//Boolesche Werte</font>
<font color="#555555">010 </font> System.out.format(<font color="#0000FF">"%b %b %2$b %1$b%n"</font>, <font color="#006699">true</font>, <font color="#006699">false</font>);
<font color="#555555">011 </font> <font color="#00AA00">//Ganzzahlen</font>
<font color="#555555">012 </font> System.out.format(<font color="#0000FF">"[%d]%n"</font>, -2517);
<font color="#555555">013 </font> System.out.format(<font color="#0000FF">"[%7d]%n"</font>, -2517);
<font color="#555555">014 </font> System.out.format(<font color="#0000FF">"[%-7d]%n"</font>, -2517);
<font color="#555555">015 </font> System.out.format(<font color="#0000FF">"[%(7d]%n"</font>, -2517);
<font color="#555555">016 </font> System.out.format(<font color="#0000FF">"[%07d]%n"</font>, -2517);
<font color="#555555">017 </font> System.out.format(<font color="#0000FF">"[%,7d]%n"</font>, -2517);
<font color="#555555">018 </font> System.out.format(<font color="#0000FF">"%1$d %<o %<x %<X%n"</font>, 127);
<font color="#555555">019 </font> <font color="#00AA00">//Fließkommazahlen</font>
<font color="#555555">020 </font> System.out.format(<font color="#0000FF">"%f%n"</font>, 0.000314);
<font color="#555555">021 </font> System.out.format(<font color="#0000FF">"%1$6.2f %1$6.2e %1$6.2E %1$6.2G%n"</font>, 3.141592);
<font color="#555555">022 </font> System.out.format(<font color="#0000FF">"%,8.2f%n"</font>, 31415.92);
<font color="#555555">023 </font> System.out.format(Locale.ENGLISH, <font color="#0000FF">"%,8.2f%n"</font>, 31415.92);
<font color="#555555">024 </font> <font color="#00AA00">//Zeichen und Strings</font>
<font color="#555555">025 </font> System.out.format(<font color="#0000FF">"%c%c%c\n"</font>, 97, 64, 98);
<font color="#555555">026 </font> System.out.format(<font color="#0000FF">"%s nein\n"</font>, <font color="#0000FF">"ja"</font>);
<font color="#555555">027 </font> <font color="#00AA00">//Datum/Uhrzeit</font>
<font color="#555555">028 </font> Calendar now = Calendar.getInstance();
<font color="#555555">029 </font> System.out.format(
<font color="#555555">030 </font> <font color="#0000FF">"%1$td.%1$tm.%1$tY %1$tH:%1$tM:%1$tS%n"</font>,
<font color="#555555">031 </font> now
<font color="#555555">032 </font> );
<font color="#555555">033 </font> System.out.format(<font color="#0000FF">"%tF%n"</font>, now);
<font color="#555555">034 </font> System.out.format(<font color="#0000FF">"%tc%n"</font>, now);
<font color="#555555">035 </font> }
<font color="#555555">036 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing1107.java"><font color="#000055" size=-1>Listing1107.java</font></a></td>
</tr>
</table>
<i>
Listing 11.7: Formatierte Ausgabe</i></p>
<p>
Seine Ausgabe ist:
<font color="#333300">
<pre>
true false false true
[-2517]
[ -2517]
[-2517 ]
[ (2517)]
[-002517]
[ -2.517]
127 177 7f 7F
0,000314
3,14 3.14e+00 3.14E+00 3.1
31.415,92
31,415.92
a@b
ja nein
02.10.2004 21:38:24
2004-10-02
Sa Okt 02 21:38:24 CEST 2004
</pre>
</font>
<hr>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage, Addison
Wesley, Version 5.0.1
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100070.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100075.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100077.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100078.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">© 1998, 2007 Guido Krüger & Thomas
Stark, <a href="http://www.javabuch.de">http://www.javabuch.de</a>
</table>
<a name="endofbody"></a>
</body>
</html>
|