-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStdIn.java
More file actions
179 lines (147 loc) · 4.9 KB
/
StdIn.java
File metadata and controls
179 lines (147 loc) · 4.9 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
/*************************************************************************
* Compilation: javac StdIn.java
* Execution: java StdIn
*
* Reads in data of various types from standard input.
*
*************************************************************************/
import java.util.Scanner;
import java.util.Locale;
import java.io.BufferedInputStream;
/**
* <i>Standard input</i>. This class provides methods for reading strings
* and numbers from standard input.
* <p>
* The Locale used is: language = English, country = US. This is consistent
* with the formatting conventions with Java floating-point literals,
* command-line arguments (via <tt>Double.parseDouble()</tt>)
* and standard output (via <tt>System.out.print()</tt>). It ensures that
* standard input works the number formatting used in the textbook.
* <p>
* For additional documentation, see <a href="http://www.cs.princeton.edu/introcs/15inout">Section 1.5</a> of
* <i>Introduction to Programming in Java: An Interdisciplinary Approach</i> by Robert Sedgewick and Kevin Wayne.
*/
public final class StdIn {
// assume Unicode UTF-8 encoding
private static String charsetName = "UTF-8";
// assume language = English, country = US for consistency with System.out.
private static Locale usLocale = new Locale("en", "US");
// the scanner object
private static Scanner scanner = new Scanner(new BufferedInputStream(System.in), charsetName);
// static initializer
static { scanner.useLocale(usLocale); }
// singleton pattern - can't instantiate
private StdIn() { }
/**
* Is there only whitespace left on standard input?
*/
public static boolean isEmpty() {
return !scanner.hasNext();
}
/**
* Return next string from standard input
*/
public static String readString() {
return scanner.next();
}
/**
* Return next int from standard input
*/
public static int readInt() {
return scanner.nextInt();
}
/**
* Return next double from standard input
*/
public static double readDouble() {
return scanner.nextDouble();
}
/**
* Return next float from standard input
*/
public static float readFloat() {
return scanner.nextFloat();
}
/**
* Return next short from standard input
*/
public static short readShort() {
return scanner.nextShort();
}
/**
* Return next long from standard input
*/
public static long readLong() {
return scanner.nextLong();
}
/**
* Return next byte from standard input
*/
public static byte readByte() {
return scanner.nextByte();
}
/**
* Return next boolean from standard input, allowing "true" or "1" for true,
* and "false" or "0" for false
*/
public static boolean readBoolean() {
String s = readString();
if (s.equalsIgnoreCase("true")) return true;
if (s.equalsIgnoreCase("false")) return false;
if (s.equals("1")) return true;
if (s.equals("0")) return false;
throw new java.util.InputMismatchException();
}
/**
* Does standard input have a next line?
*/
public static boolean hasNextLine() {
return scanner.hasNextLine();
}
/**
* Return rest of line from standard input
*/
public static String readLine() {
return scanner.nextLine();
}
/**
* Return next char from standard input
*/
// a complete hack and inefficient - email me if you have a better
public static char readChar() {
// (?s) for DOTALL mode so . matches a line termination character
// 1 says look only one character ahead
// consider precompiling the pattern
String s = scanner.findWithinHorizon("(?s).", 1);
return s.charAt(0);
}
/**
* Return rest of input from standard input
*/
public static String readAll() {
if (!scanner.hasNextLine()) return null;
// reference: http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html
return scanner.useDelimiter("\\A").next();
}
/**
* Unit test
*/
public static void main(String[] args) {
System.out.println("Type a string: ");
String s = StdIn.readString();
System.out.println("Your string was: " + s);
System.out.println();
System.out.println("Type an int: ");
int a = StdIn.readInt();
System.out.println("Your int was: " + a);
System.out.println();
System.out.println("Type a boolean: ");
boolean b = StdIn.readBoolean();
System.out.println("Your boolean was: " + b);
System.out.println();
System.out.println("Type a double: ");
double c = StdIn.readDouble();
System.out.println("Your double was: " + c);
System.out.println();
}
}