- ?
- 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
- // A version of ConsoleReader() specialised to the standard input
- // for convenience of reading text from the keyboard.
- // For example,
- //
- // j = Console.readInt(); b = Console.readBoolean();
- //
- // behaves identically to
- //
- // ConsoleReader console = new ConsoleReader();
- // j = console.readInt(); b = console.readBoolean();
- //
- // Author: J. M. Morris (jmorris@compapp.dcu.ie).
- // Version 1.5 Februaey 11th 2002.
- //
- // Example of use: Execution of the statements
- //
- // j = Console.readInt(); b = Console.readBoolean(); c = Console.readChar();
- // t = Console.readToken(); s = Console.readString();
- //
- // with input (where - represents a space, and ended with RETURN)
- //
- // ------23----TRue-X---Java123---the--rest---
- //
- // would leave j=23, b=true, c='X', t="Java123", and
- // s= "--the--rest---"
- //
- import java.io.*;
- public class Console {
- private static BufferedReader br = new BufferedReader(
- new InputStreamReader(System.in)); // the input stream
- private static String buffer = "";
- private static int p = 1; // buffer[p..] contains next input
- private static String getToken() throws IOException { // return next token from input stream
- while (buffer != null && (p>= buffer.length() ||
- Character.isWhitespace(buffer.charAt(p)))) {
- if (p>= buffer.length()) {
- buffer = br.readLine();
- p = 0;
- }
- else p++;
- }
- if (buffer == null) throw new IOException("Console: Unexpected end of file");
- int t = p;
- p++;
- while(p < buffer.length() &&
- !(Character.isWhitespace(buffer.charAt(p))))
- p++;
- p++;
- return(buffer.substring(t,p-1));
- }
- public static int readInt() {
- // Consume and return an integer. Trailing delimiter consumed.
- try {
- return Integer.parseInt(getToken());
- }
- catch (Exception e) {
- System.err.println("Console: IO Exception in readInt");
- return 0;
- }
- }
- public static boolean readBoolean() {
- // Consume and return a boolean. Trailing delimiter consumed.
- // Any string other than "true" (case ignored) is treated as false.
- try {
- return new Boolean(getToken()).booleanValue();
- }
- catch (Exception e) {
- System.err.println("Console: IO Exception in readBoolean");
- return false;
- }
- }
- public static double readDouble() {
- // Consume and return a double. Trailing delimiter consumed.
- try {
- return new Double(getToken()).doubleValue();
- }
- catch (Exception ioe) {
- System.err.println("Console: IO Exception in readDouble");
- return 0.0;
- }
- }
- public static String readToken() {
- // Consume and return a token. Trailing delimiter consumed.
- // A token is a maximal sequence of non-whitespace characters.
- // null returned on end of file
- try {
- while (buffer != null && (p>= buffer.length() ||
- Character.isWhitespace(buffer.charAt(p)))) {
- if (p>= buffer.length()) {
- buffer = br.readLine();
- p = 0;
- }
- else p++;
- }
- if (buffer == null) return null;
- int t = p;
- p++;
- while(p < buffer.length() &&
- !(Character.isWhitespace(buffer.charAt(p))))
- p++;
- p++;
- return(buffer.substring(t,p-1));
- }
- catch (IOException ioe) {
- System.err.println("Console: IO Exception in readToken");
- return "";
- }
- }
- public static char readChar() {
- //Consume and return a character (which may be an end-of-line).
- try {
- if (buffer != null && p>buffer.length()) {
- buffer = br.readLine();
- p = 0;
- }
- if (buffer == null) throw new IOException("Console: Unexpected end of file in readChar");
- if (p == buffer.length()) { // supply end-of-line
- p++;
- return('\n');
- }
- else {
- p++;
- return buffer.charAt(p-1);
- }
- }
- catch (IOException ioe) {
- System.err.println("Console: IO Exception in readChar");
- return (char)0;
- }
- }
- public static char peekChar() {
- // The next available character if any (which may be an end-of-line). The
- // character is not consumed. If buffer is empty return null character.
- if (buffer == null || p>buffer.length()) return('\000');
- else if (p == buffer.length()) return('\n');
- else return buffer.charAt(p);
- }
- public static String readString() {
- // Consume and return the remainder of current line (end-of-line discarded).
- // null returned on end of file
- try {
- if (buffer!= null && p>buffer.length()) {
- buffer = br.readLine();
- p = 0;
- }
- if (buffer == null) return null;
- int t = p; p = buffer.length() + 1;
- return buffer.substring(t);
- }
- catch (IOException ioe) {
- System.err.println("Console: IO Exception in readString");
- return "";
- }
- }
- public static int available() {
- // Number of characters available on this line (including end-of-line,
- // which counts as one character, i.e. '\n')
- if (buffer == null) return 0;
- else return (buffer.length()+1-p);
- }
- public static boolean hasMoreTokens() {
- // Are there more tokens on the current line?
- if (buffer == null) return false;
- int q = p;
- while (q<buffer.length() && Character.isWhitespace(buffer.charAt(q))) q++;
- return (q<buffer.length());
- }
- public static void skipLine() {
- // Skip any remaining input on this line.
- if (buffer != null) p = buffer.length() + 1;
- }
- public static void skipWhitespace() {
- // Consumes input until a non-whitespace character is entered (which
- // is not consumed).
- try {
- while (buffer != null && (p>= buffer.length() ||
- Character.isWhitespace(buffer.charAt(p)))) {
- if (p>= buffer.length()) {
- buffer = br.readLine();
- p = 0;
- }
- else p++;
- }
- }
- catch (IOException ioe) {
- System.err.println("Console: IO Exception in skipWhitespace");
- }
- }
- public static boolean EndOfFile() { // More characters?
- // This method is intended for use when keyboard is redirected to file
- if (available()>0) return false;
- try {
- buffer = br.readLine();
- }
- catch (IOException ioe) {
- System.err.println("Console: IO Exception in EndOfFile");
- }
- p = 0;
- return (buffer == null);
- }
- public static boolean endOfFile() { // More characters?
- // alternative spelling for EndOfFile()
- // This method is intended for use when keyboard is redirected to file
- if (available()>0) return false;
- try {
- buffer = br.readLine();
- }
- catch (IOException ioe) {
- System.err.println("Console: IO Exception in EndOfFile");
- }
- p = 0;
- return (buffer == null);
- }
- }