- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatDemo.java
More file actions
Latest commit
43 lines (36 loc) · 1.38 KB
/
Copy pathFormatDemo.java
File metadata and controls
43 lines (36 loc) · 1.38 KB
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
// console I/O with formatting strings
importjava.io.IOException;
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
importjava.io.OutputStreamWriter;
importjava.io.PrintWriter;
importjava.nio.charset.Charset;
classFormatDemo {
publicstaticvoidmain(String[] args)
throwsIOException {
// buffering container for read lines
String[] string_array = newString[5];
// charset object
Charsetcharset = Charset.defaultCharset();
// reader object
BufferedReaderreader
= newBufferedReader(newInputStreamReader(System.in, charset));
// printer object
PrintWriterprinter
= newPrintWriter(newOutputStreamWriter(System.out, charset), true);
// capture text
printer.println("Write some text [max 5 lines]:");
for (inti = 0; i < string_array.length; i++) {
string_array[i] = reader.readLine();
}
// print text
printer.println("Here is your text:");
for (Strings: string_array) {
// legacy C-style printf() could also be used here
// %n defines a platform-specific terminator (recommended!)
printer.format("This is your line: %s%n", s);
// String has a format() method as well for building formatted strings
// a la C-style sprintf
}
}
}