-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.java
More file actions
132 lines (112 loc) · 3.19 KB
/
Copy pathInput.java
File metadata and controls
132 lines (112 loc) · 3.19 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
// ReadLib is a library class of read routines all based upon pressing the enter key
// ReadLib provides methods for reading int, double, boolean, char and String types
import java.io.*;
import java.util.*;
public class Input
{
// the Tokenizer is used to get the first item typed on a line, used with
// readInt(), readDouble(), and readBoolean()
static private StringTokenizer stok;
// the BufferedReader opens the connection to the keyboard
static private BufferedReader br
= new BufferedReader (new InputStreamReader (System.in));
public static int readInt () // uses a Tokenizer and Integer wrapper class
{ // to get a true int value
int i;
try
{
String str = br.readLine ();
StringTokenizer stok = new StringTokenizer (str);
i = new Integer (stok.nextToken ()).intValue ();
}
catch (IOException ex) // connection failure
{
System.out.println (ex);
i = 0;
}
catch (NumberFormatException nfe) // invalid keyboard entry
{
System.out.println ("Warning: A non-numeric value was entered");
System.out.println ("Your variable has been given value of 0");
i = 0;
}
return i;
}
public static double readDouble ()// uses a Tokenizer and Double wrapper class
{ // to get a true double value
double d = 0;
try
{
String str = br.readLine ();
stok = new StringTokenizer (str);
d = new Double (stok.nextToken ()).doubleValue ();
}
catch (IOException io)
{
System.out.println (io);
}
catch (NumberFormatException nfe)
{
System.out.println ("Warning: A non-numeric value was entered");
System.out.println ("Your variable has been given value of 0");
d = 0;
}
return d;
}
public static boolean readBoolean () // reads a boolean, looks for trus or false
{ // assumes false otherwise
boolean result = false;
try
{
String str = br.readLine ();
stok = new StringTokenizer (str);
String answer = stok.nextToken ();
if (answer.equals ("true")) // valid true entry
{
result = true;
}
else
{
if (!answer.equals ("false")) // invalid entry
System.out.println ("An invalid value was entered, false ia assumed");
result = false;
}
}
catch (IOException io)
{
System.out.println (io);
}
return result;
}
public static char readChar ()
{
char oneChar = ' ';
try
{
String str = br.readLine ();
oneChar = str.charAt (0); // oneChar is first character of keyboard entry
}
catch (IOException io) // connection error
{
System.out.println (io);
}
catch (StringIndexOutOfBoundsException se) // enter key only, no character
{
System.out.println ("There was no character entered, blank assumed");
}
return oneChar;
}
public static String readString ()
{
String str="";
try
{
str = br.readLine (); // returns null if no characters typed
}
catch (IOException io)
{
System.out.println (io);
}
return str;
}
} // ReadLib class