-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSavitchIn.java
More file actions
executable file
·662 lines (589 loc) · 24.2 KB
/
SavitchIn.java
File metadata and controls
executable file
·662 lines (589 loc) · 24.2 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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
import java.io.*;
import java.util.*;
/**************************************************************
*Class for simple console input.
*A class designed primarily for simple keyboard input of the form
*one input value per line. If the user enters an improper input,
*i.e., an input of the wrong type or a blank line, then the user
*is prompted to reenter the input and given a brief explanation
*of what is required. Also includes some additional methods to
*input single numbers, words, and characters, without going to
*the next line.
*from:
* Walter Savitch:
* "Java - an Introduction to Computer Science & Programming"
* Prentice Hall 2001
*************************************************************/
public class SavitchIn
{
/**********************************************************
*Reads a line of text and returns that line as a String value.
*The end of a line must be indicated either by a new-line
*character '\n' or by a carriage return '\r' followed by a
*new-line character '\n'. (Almost all systems do this
*automatically. So, you need not worry about this detail.)
*Neither the '\n', nor the '\r' if present, are part of the
*string returned. This will read the rest of a line if the
*line is already partially read.
*********************************************************/
public static String readLine()
{
char nextChar;
String result = "";
boolean done = false;
while (!done)
{
nextChar = readChar();
if (nextChar == '\n')
done = true;
else if (nextChar == '\r')
{
//Do nothing.
//Next loop iteration will detect '\n'
}
else
result = result + nextChar;
}
return result;
}
/*********************************************************
*Reads the first string of nonwhite characters on a line and
*returns that string. The rest of the line is discarded. If
*the line contains only white space, then the user is asked
*to reenter the line.
*********************************************************/
public static String readLineWord()
{
String inputString = null,
result = null;
boolean done = false;
while(!done)
{
inputString = readLine();
StringTokenizer wordSource =
new StringTokenizer(inputString);
if (wordSource.hasMoreTokens())
{
result = wordSource.nextToken();
done = true;
}
else
{
System.out.println(
"Your input is not correct. Your input must");
System.out.println(
"contain at least one nonwhitespace character.");
System.out.println(
"Please, try again. Enter input:");
}
}
return result;
}
/**********************************************************
*Precondition: The user has entered a number of type int on
*a line by itself, except that there may be white space before
*and/or after the number.
*Action: Reads and returns the number as a value of type int.
*The rest of the line is discarded. If the input is not
*entered correctly, then in most cases, the user will be
*asked to reenter the input. In particular, this applies to
*incorrect number formats and blank lines.
*********************************************************/
public static int readLineInt()
{
String inputString = null;
int number = -9999;//To keep the compiler happy.
//Designed to look like a garbage value.
boolean done = false;
while (! done)
{
try
{
inputString = readLine();
inputString = inputString.trim();
number = Integer.parseInt(inputString);
done = true;
}
catch (NumberFormatException e)
{
System.out.println(
"Your input number is not correct.");
System.out.println("Your input number must be");
System.out.println("a whole number written as an");
System.out.println("ordinary numeral, such as 42");
System.out.println("Minus signs are OK,"
+ "but do not use a plus sign.");
System.out.println("Please, try again.");
System.out.println("Enter a whole number:");
}
}
return number;
}
/*********************************************************
*Precondition: The user has entered a number of type long on
*a line by itself, except that there may be white space
*before and/or after the number.
*Action: Reads and returns the number as a value of type
*long. The rest of the line is discarded. If the input is not
*entered correctly, then in most cases, the user will be asked
*to reenter the input. In particular, this applies to
*incorrect number formats and blank lines.
********************************************************/
public static long readLineLong()
{
String inputString = null;
long number = -9999;//To keep the compiler happy.
//Designed to look like a garbage value.
boolean done = false;
while (! done)
{
try
{
inputString = readLine();
inputString = inputString.trim();
number = Long.parseLong(inputString);
done = true;
}
catch (NumberFormatException e)
{
System.out.println(
"Your input number is not correct.");
System.out.println("Your input number must be");
System.out.println("a whole number written as an");
System.out.println("ordinary numeral, such as 42");
System.out.println("Minus signs are OK,"
+ "but do not use a plus sign.");
System.out.println("Please, try again.");
System.out.println("Enter a whole number:");
}
}
return number;
}
/********************************************************
*Precondition: The user has entered a number of type double
*on a line by itself, except that there may be white space
*before and/or after the number.
*Action: Reads and returns the number as a value of type
*double. The rest of the line is discarded. If the input is
*not entered correctly, then in most cases, the user will be
*asked to reenter the input. In particular, this applies to
*incorrect number formats and blank lines.
*********************************************************/
public static double readLineDouble()
{
String inputString = null;
double number = -9999;//To keep the compiler happy.
//Designed to look like a garbage value.
boolean done = false;
while (! done)
{
try
{
inputString = readLine();
inputString = inputString.trim();
number = Double.parseDouble(inputString);
done = true;
}
catch (NumberFormatException e)
{
System.out.println(
"Your input number is not correct.");
System.out.println("Your input number must be");
System.out.println("an ordinary number either with");
System.out.println("or without a decimal point,");
System.out.println("such as 42 or 9.99");
System.out.println("Please, try again.");
System.out.println("Enter the number:");
}
}
return number;
}
/*********************************************************
*Precondition: The user has entered a number of type float
*on a line by itself, except that there may be white space
*before and/or after the number.
*Action: Reads and returns the number as a value of type
*float. The rest of the line is discarded. If the input is
*not entered correctly, then in most cases, the user will
*be asked to reenter the input. In particular,
*this applies to incorrect number formats and blank lines.
*********************************************************/
public static float readLineFloat()
{
String inputString = null;
float number = -9999;//To keep the compiler happy.
//Designed to look like a garbage value.
boolean done = false;
while (! done)
{
try
{
inputString = readLine();
inputString = inputString.trim();
number = Float.parseFloat(inputString);
done = true;
}
catch (NumberFormatException e)
{
System.out.println(
"Your input number is not correct.");
System.out.println("Your input number must be");
System.out.println("an ordinary number either with");
System.out.println("or without a decimal point,");
System.out.println("such as 42 or 9.99");
System.out.println("Please, try again.");
System.out.println("Enter the number:");
}
}
return number;
}
/*********************************************************
*Reads the first nonwhite character on a line and returns
*that character. The rest of the line is discarded. If the
*line contains only white space, then the user is asked to
*reenter the line.
*********************************************************/
public static char readLineNonwhiteChar()
{
boolean done = false;
String inputString = null;
char nonWhite = ' ';//To keep the compiler happy.
while (! done)
{
inputString = readLine();
inputString = inputString.trim();
if (inputString.length() == 0)
{
System.out.println(
"Your input is not correct.");
System.out.println("Your input must contain at");
System.out.println(
"least one nonwhitespace character.");
System.out.println("Please, try again.");
System.out.println("Enter input:");
}
else
{
nonWhite = (inputString.charAt(0));
done = true;
}
}
return nonWhite;
}
/********************************************************
*Input should consist of a single word on a line, possibly
*surrounded by white space. The line is read and discarded.
*If the input word is "true" or "t", then true is returned.
*If the input word is "false" or "f", then false is returned.
*Uppercase and lowercase letters are considered equal. If the
*user enters anything else (e.g., multiple words or different
*words), then the user is asked to reenter the input.
********************************************************/
public static boolean readLineBoolean()
{
boolean done = false;
String inputString = null;
boolean result = false;//To keep the compiler happy.
while (! done)
{
inputString = readLine();
inputString = inputString.trim();
if (inputString.equalsIgnoreCase("true")
|| inputString.equalsIgnoreCase("t"))
{
result = true;
done = true;
}
else if (inputString.equalsIgnoreCase("false")
|| inputString.equalsIgnoreCase("f"))
{
result = false;
done = true;
}
else
{
System.out.println(
"Your input number is not correct.");
System.out.println("Your input must be");
System.out.println("one of the following:");
System.out.println("the word true,");
System.out.println("the word false,");
System.out.println("the letter T,");
System.out.println("or the letter F.");
System.out.println("You may use either upper-");
System.out.println("or lowercase letters.");
System.out.println("Please, try again.");
System.out.println("Enter input:");
}
}
return result;
}
/********************************************************
*Reads the next input character and returns that character. The
*next read takes place on the same line where this one left off.
********************************************************/
public static char readChar()
{
int charAsInt = -1; //To keep the compiler happy
try
{
charAsInt = System.in.read();
}
catch(IOException e)
{
System.out.println(e.getMessage());
System.out.println("Fatal error. Ending Program.");
System.exit(0);
}
return (char)charAsInt;
}
/*******************************************************
*Reads the next nonwhite input character and returns that
*character. The next read takes place immediately after
*the character read.
*******************************************************/
public static char readNonwhiteChar()
{
char next;
next = readChar();
while (Character.isWhitespace(next))
next = readChar();
return next;
}
/**********************************************************
*The following methods are not used in the text, except for
*a brief reference in Chapter 2. No program code uses them.
*However, some programmers may want to use them.
*********************************************************/
/**********************************************************
*Precondition: The next input in the stream consists of an
*int value, possibly preceded by white space, but definitely
*followed by white space.
*Action: Reads the first string of nonwhite characters
*and returns the int value it represents. Discards the first
*whitespace character after the word. The next read takes
*place immediately after the discarded whitespace.
*In particular, if the word is at the end of a line, the
*next reading will take place starting on the next line.
*If the next word does not represent an int value,
*a NumberFormatException is thrown.
*********************************************************/
public static int readInt() throws NumberFormatException
{
String inputString = null;
inputString = readWord();
return Integer.parseInt(inputString);
}
/**********************************************************
*Precondition: The next input consists of a long value,
*possibly preceded by white space, but definitely
*followed by white space.
*Action: Reads the first string of nonwhite characters and
*returns the long value it represents. Discards the first
*whitespace character after the string read. The next read
*takes place immediately after the discarded whitespace.
*In particular, if the string read is at the end of a line,
*the next reading will take place starting on the next line.
*If the next word does not represent a long value,
*a NumberFormatException is thrown.
**********************************************************/
public static long readLong()
throws NumberFormatException
{
String inputString = null;
inputString = readWord();
return Long.parseLong(inputString);
}
/**********************************************************
*Precondition: The next input consists of a double value,
*possibly preceded by white space, but definitely
*followed by white space.
*Action: Reads the first string of nonwhitespace characters
*and returns the double value it represents. Discards the
*first whitespace character after the string read. The next
*read takes place immediately after the discarded whitespace.
*In particular, if the string read is at the end of a line,
*the next reading will take place starting on the next line.
*If the next word does not represent a double value,
*a NumberFormatException is thrown.
*********************************************************/
public static double readDouble()
throws NumberFormatException
{
String inputString = null;
inputString = readWord();
return Double.parseDouble(inputString);
}
/*********************************************************
*Precondition: The next input consists of a float value,
*possibly preceded by white space, but definitely
*followed by white space.
*Action: Reads the first string of nonwhite characters and
*returns the float value it represents. Discards the first
*whitespace character after the string read. The next read
*takes place immediately after the discarded whitespace.
*In particular, if the string read is at the end of a line,
*the next reading will take place starting on the next line.
*If the next word does not represent a float value,
*a NumberFormatException is thrown.
*********************************************************/
public static float readFloat()
throws NumberFormatException
{
String inputString = null;
inputString = readWord();
return Float.parseFloat(inputString);
}
/**********************************************************
*Reads the first string of nonwhite characters and returns
*that string. Discards the first whitespace character after
*the string read. The next read takes place immediately after
*the discarded whitespace. In particular, if the string
*read is at the end of a line, the next reading will take
*place starting on the next line. Note, that if it receives
*blank lines, it will wait until it gets a nonwhitespace
*character.
*********************************************************/
public static String readWord()
{
String result = "";
char next;
next = readChar();
while (Character.isWhitespace(next))
next = readChar();
while (!(Character.isWhitespace(next)))
{
result = result + next;
next = readChar();
}
if (next == '\r')
{
next = readChar();
if (next != '\n')
{
System.out.println(
"Fatal Error in method readWord of class SavitchIn.");
System.exit(1);
}
}
return result;
}
/*********************************************************
*Precondition: The user has entered a number of type byte on
*a line by itself, except that there may be white space before
*and/or after the number.
*Action: Reads and returns the number as a value of type byte.
*The rest of the line is discarded. If the input is not
*entered correctly, then in most cases, the user will be
*asked to reenter the input. In particular, this applies to
*incorrect number formats and blank lines.
*********************************************************/
public static byte readLineByte()
{
String inputString = null;
byte number = -123;//To keep the compiler happy.
//Designed to look like a garbage value.
boolean done = false;
while (! done)
{
try
{
inputString = readLine();
inputString = inputString.trim();
number = Byte.parseByte(inputString);
done = true;
}
catch (NumberFormatException e)
{
System.out.println(
"Your input number is not correct.");
System.out.println("Your input number must be a");
System.out.println("whole number in the range");
System.out.println("-128 to 127, written as");
System.out.println("an ordinary numeral, such as 42.");
System.out.println("Minus signs are OK,"
+ "but do not use a plus sign.");
System.out.println("Please, try again.");
System.out.println("Enter a whole number:");
}
}
return number;
}
/*********************************************************
*Precondition: The user has entered a number of type short on
*a line by itself, except that there may be white space before
*and/or after the number.
*Action: Reads and returns the number as a value of type short.
*The rest of the line is discarded. If the input is not
*entered correctly, then in most cases, the user will be
*asked to reenter the input. In particular, this applies to
*incorrect number formats and blank lines.
********************************************************/
public static short readLineShort()
{
String inputString = null;
short number = -9999;//To keep the compiler happy.
//Designed to look like a garbage value.
boolean done = false;
while (! done)
{
try
{
inputString = readLine();
inputString = inputString.trim();
number = Short.parseShort(inputString);
done = true;
}
catch (NumberFormatException e)
{
System.out.println(
"Your input number is not correct.");
System.out.println("Your input number must be a");
System.out.println("whole number in the range");
System.out.println("-32768 to 32767, written as");
System.out.println("an ordinary numeral, such as 42.");
System.out.println("Minus signs are OK,"
+ "but do not use a plus sign.");
System.out.println("Please, try again.");
System.out.println("Enter a whole number:");
}
}
return number;
}
public static byte readByte() throws NumberFormatException
{
String inputString = null;
inputString = readWord();
return Byte.parseByte(inputString);
}
public static short readShort() throws NumberFormatException
{
String inputString = null;
inputString = readWord();
return Short.parseShort(inputString);
}
//The following was intentionally not used in the code for
//other methods so that somebody reading the code could more
//quickly see what was being used.
/*********************************************************
*Reads the first byte in the input stream and returns that
*byte as an int. The next read takes place where this one
*left off. This read is the same as System.in.read(),
*except that it catches IOExceptions.
********************************************************/
public static int read()
{
int result = -1; //To keep the compiler happy
try
{
result = System.in.read();
}
catch(IOException e)
{
System.out.println(e.getMessage());
System.out.println("Fatal error. Ending Program.");
System.exit(0);
}
return result;
}
}