|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +/** |
| 4 | + * This class provides static methods for easily reading |
| 5 | + * input from STDIN: |
| 6 | + * |
| 7 | + * nextLine reads a string |
| 8 | + * nextInt reads an integer |
| 9 | + * nextDouble reads a double |
| 10 | + * nextChar reads a character |
| 11 | + * |
| 12 | + * All methods consume the end-of-line character. |
| 13 | + */ |
| 14 | +public class In { |
| 15 | + /** |
| 16 | + * A singleton instance of Scanner used for reading all input |
| 17 | + * from STDIN. |
| 18 | + */ |
| 19 | + private static final Scanner scanner = new Scanner(System.in); |
| 20 | + |
| 21 | + /** |
| 22 | + * The constructor is private because no instances of this |
| 23 | + * class should be created. All methods are static and can |
| 24 | + * be directly invoked on the class itself. |
| 25 | + */ |
| 26 | + private In() {} |
| 27 | + |
| 28 | + /** |
| 29 | + * Read the next line of text. |
| 30 | + * |
| 31 | + * @return the line as a String |
| 32 | + */ |
| 33 | + public static String nextLine() { |
| 34 | + return scanner.nextLine(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Read the next line as an integer. |
| 39 | + * |
| 40 | + * @return the integer that was read |
| 41 | + */ |
| 42 | + public static int nextInt() { |
| 43 | + int value = scanner.nextInt(); |
| 44 | + scanner.nextLine(); // read the "\n" as well |
| 45 | + return value; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Read the next line as a double. |
| 50 | + * |
| 51 | + * @return the double that was read |
| 52 | + */ |
| 53 | + public static double nextDouble() { |
| 54 | + double value = scanner.nextDouble(); |
| 55 | + scanner.nextLine(); |
| 56 | + return value; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Read the first character of the next line of text. |
| 61 | + * |
| 62 | + * @return the character that was read |
| 63 | + */ |
| 64 | + public static char nextChar() { |
| 65 | + return scanner.nextLine().charAt(0); |
| 66 | + } |
| 67 | +} |
0 commit comments