-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOUtil.java
94 lines (76 loc) · 2.27 KB
/
IOUtil.java
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
import java.io.*;
public class IOUtil {
private static StreamTokenizer tokenizer = new StreamTokenizer(
new InputStreamReader(System.in));
public static double readDouble() {
tokenizer.resetSyntax();
tokenizer.whitespaceChars(0, ' ');
tokenizer.wordChars(33, 255);
try {
if (tokenizer.nextToken() == StreamTokenizer.TT_EOF) {
throw new IOException("End Of File found.");
} else {
return Double.parseDouble(tokenizer.sval);
}
} catch (NumberFormatException nfe) {
return readDouble();
} catch (java.io.IOException e) {
throw new RuntimeException(e);
}
}
public static float readFloat() {
tokenizer.resetSyntax();
tokenizer.whitespaceChars(0, ' ');
tokenizer.wordChars(33, 255);
try {
if (tokenizer.nextToken() == StreamTokenizer.TT_EOF) {
throw new IOException("End Of File found.");
} else {
return Float.parseFloat(tokenizer.sval);
}
} catch (NumberFormatException nfe) {
return readFloat();
} catch (java.io.IOException e) {
throw new RuntimeException(e);
}
}
public static int readInt() {
try {
tokenizer.resetSyntax();
tokenizer.whitespaceChars(0, ' ');
tokenizer.wordChars(33, 255);
if (tokenizer.nextToken() == StreamTokenizer.TT_EOF) {
throw new IOException("End Of File found.");
} else {
return (int) Double.parseDouble(tokenizer.sval);
}
} catch (NumberFormatException nfe) {
return readInt();
} catch (java.io.IOException e) {
throw new RuntimeException(e);
}
}
public static String readString() {
try {
tokenizer.resetSyntax();
tokenizer.whitespaceChars(0, ' ');
tokenizer.wordChars(33, 255);
if (tokenizer.nextToken() == StreamTokenizer.TT_EOF) {
throw new IOException("End Of File found.");
} else {
return tokenizer.sval;
}
} catch (java.io.IOException e) {
throw new RuntimeException(e);
}
}
public static String readLine() {
try {
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
return(in.readLine());
} catch (IOException e) {
return null;
}
}
}