-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParsingMixedData.java
More file actions
44 lines (33 loc) · 1.06 KB
/
ParsingMixedData.java
File metadata and controls
44 lines (33 loc) · 1.06 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
44
/**
* This class would parse mixed data.
*
*
*/
package scanner;
import java.util.*;
import java.io.*;
public class ParsingMixedData {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("scan-mixed-data.txt");
Scanner sc = new Scanner(fr);
FileWriter fw = new FileWriter("scn-mixed-data-output.txt");
while (sc.hasNextLine()) {
String line = sc.nextLine(); // parse one line at a time
Scanner scanLine = new Scanner(line); // second line as scanner
// break each line for to seek out mixed data
while (scanLine.hasNext()) {
if (scanLine.hasNextInt())
fw.write("integer: " + scanLine.nextInt() + " | ");
else if (scanLine.hasNextDouble())
fw.write("double: " + scanLine.nextDouble() + " | ");
else if (scanLine.hasNextBoolean())
fw.write("boolean: " + scanLine.nextBoolean() + " | ");
else
fw.write("string: " + scanLine.next() + " | ");
} // end of inner while
fw.write("\n");
} // end of while
fw.close();
sc.close();
} // end of main()
} // end of class