-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyboard.java
201 lines (177 loc) Β· 6.51 KB
/
Keyboard.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
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
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.InputStreamReader;
public class Keyboard {
private static String line = null; // line read from input.
private static int pos = 0; // Position of next char in input line, not processed yet.
private final static Pattern integerRegex = Pattern.compile("(\\+|-)?[0-9]+");
private static int inErrorCount; // Number of consecutive errors on standard input; reset to 0 when a successful read occurs.
private final static BufferedReader standardIn = new BufferedReader(new InputStreamReader(System.in)); // wraps standard input stream
private final static PrintWriter standardOut = new PrintWriter(System.out); // wraps standard output stream
private static boolean readingStandardIn = true;
private static boolean writingStandardOut = true;
private static BufferedReader in = standardIn; // Stream that data is read from.
private static PrintWriter out = standardOut; // Stream that data is written to.
private static Matcher integerMatcher; // For reading integer numbers; created from the integer Regex Pattern.
/**
* Skips whitespace characters and then reads a value of type char from input, discarding the rest of
* the current line of input.
*/
public static char getlnChar() {
char ch=getChar();
emptyLine();
return ch;
}
/**
* discard the rest of the current line of input
*/
private static void emptyLine() {
line = null;
}
/**
* Skips whitespace characters and then reads a single non-whitespace character from input.
*/
public static char getChar() {
skipWhitespace();
return readChar();
}
/**
* Skips over any whitespace characters, including for end-of-lines.
*/
private static void skipWhitespace() {
char ch=nextChar();
while ( Character.isWhitespace(ch)) {
readChar();
if (ch == '\n' && readingStandardIn && writingStandardOut) {
out.print("? ");
out.flush();
}
ch = nextChar();
}
}
/**
* return next character from input
*/
private static char nextChar () {
if (line == null || pos > line.length())
fillLine();
if (pos == line.length())
return '\n';
else
return line.charAt(pos);
}
/**
* returns and discards next character from input
*/
private static char readChar() {
char ch = nextChar ();
if (line == null) {
if (readingStandardIn)
throw new IllegalArgumentException("Illegal Argument");
}
pos++;
return ch;
}
/**
* Type a line and return,
*/
private static void fillLine() {
try {
line = in.readLine();
}
catch (Exception e) {
if (readingStandardIn)
throw new IllegalArgumentException("Standard input error");
}
pos = 0;
integerMatcher = null;
}
/**
* Skips whitespace characters and then reads a value of type int from input, discarding the rest of
* the current line of input (including the next end-of-line character, if any).
*/
public static int getlnInt() {
int n=getInt();
emptyLine();
return n;
}
/**
* Skips whitespace characters and then reads a value of type int from input.
*/
public static int getInt() {
return (int)readInteger(Integer.MIN_VALUE, Integer.MAX_VALUE);
}
private static long readInteger(long min, long max) { // read integer in specified range
long k=0;
while (true) {
String s = readIntegerString();
if (s == null){
errorMessage("Input value not found.",
"Integer in the range " + min + " to " + max);
}
else {
String str = s.toString();
try {
k = Long.parseLong(str);
}
catch (NumberFormatException e) {
errorMessage("Illegal input, " + str + ".",
"Integer in the range " + min + " to " + max);
continue;
}
if (k < min || k > max) {
errorMessage("Input out off range, " + str + ".",
"Integer in the range " + min + " to " + max);
continue;
}
break;
}
}
inErrorCount = 0;
return k;
}
/**
* read chars from input following syntax of integers
*/
private static String readIntegerString() {
skipWhitespace();
if (integerMatcher == null)
integerMatcher = integerRegex.matcher(line);
integerMatcher.region(pos,line.length());
if (integerMatcher.lookingAt()) {
String str = integerMatcher.group();
pos = integerMatcher.end();
return str;
}
else
return null;
}
/**
* Report error on input.
*/
private static void errorMessage(String message, String wanted) {
if (readingStandardIn && writingStandardOut) {
// inform user of error and ask user to re-enter.
out.println();
out.print(" *** Input error : " + message + "\n");
out.print(" *** Wanted: " + wanted + "\n");
if (nextChar () == '\n')
out.print("(end-of-line)\n\n");
else {
while (nextChar () != '\n') // Discard and echo remaining chars on the current line of input.
out.print(readChar());
out.print("\n\n");
}
out.print("Please re-enter: ");
out.flush();
readChar(); // discard the end-of-line character
inErrorCount++;
if (inErrorCount >= 10)
throw new IllegalArgumentException("Standard input errors.");
}
else
throw new IllegalArgumentException("Input stream error:\n"
+ message + "\nWanted " + wanted);
}
}