-
Notifications
You must be signed in to change notification settings - Fork 0
/
In.java
67 lines (61 loc) · 1.37 KB
/
In.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
import java.util.*;
/**
* This class provides static methods for easily reading
* input from STDIN:
*
* nextLine reads a string
* nextInt reads an integer
* nextDouble reads a double
* nextChar reads a character
*
* All methods consume the end-of-line character.
*/
public class In {
/**
* A singleton instance of Scanner used for reading all input
* from STDIN.
*/
private static final Scanner scanner = new Scanner(System.in);
/**
* The constructor is private because no instances of this
* class should be created. All methods are static and can
* be directly invoked on the class itself.
*/
private In() {}
/**
* Read the next line of text.
*
* @return the line as a String
*/
public static String nextLine() {
return scanner.nextLine();
}
/**
* Read the next line as an integer.
*
* @return the integer that was read
*/
public static int nextInt() {
int value = scanner.nextInt();
scanner.nextLine(); // read the "\n" as well
return value;
}
/**
* Read the next line as a double.
*
* @return the double that was read
*/
public static double nextDouble() {
double value = scanner.nextDouble();
scanner.nextLine();
return value;
}
/**
* Read the first character of the next line of text.
*
* @return the character that was read
*/
public static char nextChar() {
return scanner.nextLine().charAt(0);
}
}