forked from Jkutkut/Java-SuperScanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SuperScanner.java
453 lines (370 loc) · 12.5 KB
/
SuperScanner.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package jkutkut;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.Scanner;
/**
* Custom wrapper for the java.util.Scanner class able to error-handle the input given to it.
* It features a few methods to make the interaction with the user easier.
* Due to the fact that the Scanner class is <i>final</i>, it is not possible to
* extend it directly.
*
* @author "Jkutkut -- Jorge Re"
*/
public abstract class SuperScanner {
private final Scanner sc;
// ******* Constructors (Same as original) *******
public SuperScanner(File source) throws FileNotFoundException {
sc = new Scanner(source);
}
public SuperScanner(InputStream in) {
sc = new Scanner(in);
}
public SuperScanner(Path source) throws IOException {
sc = new Scanner(source);
}
public SuperScanner(Readable source) {
sc = new Scanner(source);
}
public SuperScanner(ReadableByteChannel source) {
sc = new Scanner(source);
}
public SuperScanner(String str) {
sc = new Scanner(str);
}
public SuperScanner(File source, Charset charset) throws IOException {
sc = new Scanner(source, charset);
}
public SuperScanner(File source, String charsetName) throws IOException {
sc = new Scanner(source, charsetName);
}
public SuperScanner(ReadableByteChannel source, Charset charset) {
sc = new Scanner(source, charset);
}
public SuperScanner(ReadableByteChannel source, String charsetName) {
sc = new Scanner(source, charsetName);
}
/**
* Close the scanner.
*/
public void close() {
sc.close();
}
// ******* Methods to obtain input ********
// ******* String ********
/**
* Get a String from the user.
* @param question - Question to show using System.out
* @return Response given by the scanner.
*/
public String getString(String question) {
System.out.print(question);
return sc.nextLine();
}
/**
* Get a String from the user.
* @param question - Question to show using System.out
* @param minLen - min length of String
* @param maxLen - max length of String
* @return Response given by the scanner meeting the criteria.
*/
public String getString(String question, int minLen, int maxLen) {
String str;
while (true) {
str = getString(question);
if (str.length() < minLen)
System.out.println(getErrorMinLenString(minLen));
else if (str.length() > maxLen)
System.out.println(getErrorMaxLenString(maxLen));
else
return str;
}
}
protected abstract String getErrorMinLenString(int minLen);
protected abstract String getErrorMaxLenString(int minLen);
/**
* Get a String from the user that is in the given array.
* @param question - Question to show using System.out
* @param options - Array of Strings to compare the response with.
* @return Response given by the scanner meeting the criteria.
*/
public String getStringIn(String question, String[] options) {
if (options.length == 0)
throw new IllegalArgumentException(getErrorNoOptions());
String str;
question += " [" + String.join(", ", options) + "] ";
while (true) {
str = getString(question);
for (String option : options)
if (str.equals(option))
return str;
System.out.println(getErrorInvalidOption());
}
}
protected abstract String getErrorNoOptions();
protected abstract String getErrorInvalidOption();
public String getFileName(String question) {
String str;
while (true) {
str = getString(question, 1, Integer.MAX_VALUE);
if (new File(str).exists())
return str;
System.out.println(getErrorFileNotFound());
}
}
protected abstract String getErrorFileNotFound();
// ******* Integer ********
/**
* Get an int from the user.
* @param question - Question to show using System.out.print
* @return Integer given by Scanner
*/
public int getInt(String question) {
while (true) {
try {
System.out.print(question);
return Integer.parseInt(sc.nextLine());
}
catch (NumberFormatException e) {
System.out.println(getErrorNotInt());
}
}
}
protected abstract String getErrorNotInt();
/**
* Get a positive int from the user.
* @param question - Question to show using System.out
* @return Integer greater or equal to 0
*/
public int getNatural(String question) {
int n = 0;
boolean isNotNatural = true;
while (isNotNatural) {
n = getInt(question);
if (n >= 0)
isNotNatural = false;
else
System.out.println(getErrorNotNatural());
}
return n;
}
protected abstract String getErrorNotNatural();
/**
* Get an int in the given range.
* @param question - Question to show using System.out
* @param min - min value of the desired int
* @param max - max value of the desired int
* @return Integer inside the interval [min, max]
*/
public int getIntInRange(String question, int min, int max) {
if (min > max) {
int swap = min;
min = max;
max = swap;
}
int n = 0;
boolean isNotValid = true;
while (isNotValid) {
n = getInt(question);
if (n >= min && n <= max)
isNotValid = false;
else
System.out.println(getErrorIntNotInRange(min, max));
}
return n;
}
protected abstract String getErrorIntNotInRange(int min, int max);
// ******* Float ********
/**
* Get a float from the user.
* @param question - Question to show using System.out.print
* @return Float given by Scanner
*/
public float getFloat(String question) {
while (true) {
try {
System.out.print(question);
return Float.parseFloat(sc.nextLine());
}
catch (NumberFormatException e) {
System.out.println(getErrorNotFloat());
}
}
}
protected abstract String getErrorNotFloat();
/**
* Get a positive float from the user.
* @param question - Question to show using System.out
* @param min - min value of the desired int
* @param max - max value of the desired int
* @return Float inside the interval [min, max]
*/
public float getFloatInRange(String question, float min, float max) {
if (min > max) {
float swap = min;
min = max;
max = swap;
}
float n = 0;
boolean isNotValid = true;
while (isNotValid) {
n = getFloat(question);
if (n >= min && n <= max)
isNotValid = false;
else
System.out.println(getErrorFloatNotInRange(min, max));
}
return n;
}
protected abstract String getErrorFloatNotInRange(float min, float max);
// ******* Implementations ********
public static class En extends SuperScanner {
// ******* Constructors (Same as original) *******
public En(File source) throws FileNotFoundException {
super(source);
}
public En(InputStream in) {
super(in);
}
public En(Path source) throws IOException {
super(source);
}
public En(Readable source) {
super(source);
}
public En(ReadableByteChannel source) {
super(source);
}
public En(String str) {
super(str);
}
public En(File source, Charset charset) throws IOException {
super(source, charset);
}
public En(File source, String charsetName) throws IOException {
super(source, charsetName);
}
public En(ReadableByteChannel source, Charset charset) {
super(source, charset);
}
public En(ReadableByteChannel source, String charsetName) {
super(source, charsetName);
}
@Override
protected String getErrorMinLenString(int minLen) {
return String.format("The string must have at least %d characters.", minLen);
}
@Override
protected String getErrorMaxLenString(int minLen) {
return String.format("The string must have at most %d characters.", minLen);
}
@Override
protected String getErrorNoOptions() {
return "There are no options to choose from.";
}
@Override
protected String getErrorInvalidOption() {
return "The option is not valid.";
}
@Override
protected String getErrorFileNotFound() {
return "The file does not exist.";
}
@Override
protected String getErrorNotInt() {
return "The value is not a valid integer.";
}
@Override
protected String getErrorNotNatural() {
return "The number must be a natural -> [0, inf)";
}
@Override
protected String getErrorIntNotInRange(int min, int max) {
return String.format("The number must be an integer in the range [%d, %d]", min, max);
}
@Override
protected String getErrorNotFloat() {
return "The value is not a valid float.";
}
@Override
protected String getErrorFloatNotInRange(float min, float max) {
return String.format("The number must be a float in the range [%f, %f]", min, max);
}
}
public static class Es extends SuperScanner {
public Es(File source) throws FileNotFoundException {
super(source);
}
public Es(InputStream in) {
super(in);
}
public Es(Path source) throws IOException {
super(source);
}
public Es(Readable source) {
super(source);
}
public Es(ReadableByteChannel source) {
super(source);
}
public Es(String str) {
super(str);
}
public Es(File source, Charset charset) throws IOException {
super(source, charset);
}
public Es(File source, String charsetName) throws IOException {
super(source, charsetName);
}
public Es(ReadableByteChannel source, Charset charset) {
super(source, charset);
}
public Es(ReadableByteChannel source, String charsetName) {
super(source, charsetName);
}
@Override
protected String getErrorMinLenString(int minLen) {
return String.format("La cadena debe tener al menos %d caracteres.", minLen);
}
@Override
protected String getErrorMaxLenString(int minLen) {
return String.format("La cadena debe tener al menos %d caracteres.", minLen);
}
@Override
protected String getErrorNoOptions() {
return "No hay opciones para elegir.";
}
@Override
protected String getErrorInvalidOption() {
return "La opción no es válida.";
}
@Override
protected String getErrorFileNotFound() {
return "El archivo no existe.";
}
@Override
protected String getErrorNotInt() {
return "El valor no es un entero válido.";
}
@Override
protected String getErrorNotNatural() {
return "El número debe ser natural -> [0, inf)";
}
@Override
protected String getErrorIntNotInRange(int min, int max) {
return String.format("El número debe ser un entero en el rango [%d, %d]", min, max);
}
@Override
protected String getErrorNotFloat() {
return "El valor no es un float válido.";
}
@Override
protected String getErrorFloatNotInRange(float min, float max) {
return String.format("El número debe ser un float en el rango [%f, %f]", min, max);
}
}
}