|
| 1 | +package blah; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.math.BigDecimal; |
| 7 | +import java.math.BigInteger; |
| 8 | + |
| 9 | +public class Input { |
| 10 | + private static final BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); |
| 11 | + |
| 12 | + |
| 13 | + /** |
| 14 | + * Reads an int number |
| 15 | + * |
| 16 | + * @return An int containing the value of the input |
| 17 | + * @throws RuntimeException if there is an exception while parsing the number or during input |
| 18 | + */ |
| 19 | + public static int inputInt() { |
| 20 | + try { |
| 21 | + return Integer.parseInt(in.readLine()); |
| 22 | + } catch (IOException e) { |
| 23 | + throw new RuntimeException(e); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Reads an int after showing a message |
| 29 | + * |
| 30 | + * @param msg The message to be displayed before reading the input integer |
| 31 | + * @return An int containing the value of the input |
| 32 | + * @throws RuntimeException if there is an exception while parsing the number or during input |
| 33 | + */ |
| 34 | + public static int inputInt(String msg) { |
| 35 | + try { |
| 36 | + System.out.print(msg); |
| 37 | + return Integer.parseInt(in.readLine()); |
| 38 | + } catch (IOException e) { |
| 39 | + throw new RuntimeException(e); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Reads a string from the input till the first occurrence of a whitespace |
| 45 | + * |
| 46 | + * @return A String containing the value of the input |
| 47 | + * @throws RuntimeException if there is an exception while parsing the string or during input |
| 48 | + */ |
| 49 | + public static String input() { |
| 50 | + try { |
| 51 | + return in.readLine().split(" ")[0]; |
| 52 | + } catch (IOException e) { |
| 53 | + throw new RuntimeException(e); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Reads a string from the input till the first occurrence of a whitespace after displaying a message |
| 59 | + * |
| 60 | + * @param msg The message to be displayed before reading the input string |
| 61 | + * @return A String containing the value of the input |
| 62 | + * @throws RuntimeException if there is an exception while parsing the string or during input |
| 63 | + */ |
| 64 | + public static String input(String msg) { |
| 65 | + try { |
| 66 | + System.out.print(msg); |
| 67 | + return in.readLine().split(" ")[0]; |
| 68 | + } catch (IOException e) { |
| 69 | + throw new RuntimeException(e); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Reads a string from the input till the first occurrence of a new line character '\n' |
| 75 | + * |
| 76 | + * @return A String containing the value of the input |
| 77 | + * @throws RuntimeException if there is an exception while parsing the string or during input |
| 78 | + */ |
| 79 | + public static String inputString() { |
| 80 | + try { |
| 81 | + return in.readLine(); |
| 82 | + } catch (IOException e) { |
| 83 | + throw new RuntimeException(e); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Reads a string from the input till the first occurrence of a new line character '\n' after displaying a message |
| 89 | + * |
| 90 | + * @param msg The message to be displayed before reading the input string |
| 91 | + * @return A String containing the value of the input |
| 92 | + * @throws RuntimeException if there is an exception while parsing the string or during input |
| 93 | + */ |
| 94 | + public static String inputString(String msg) { |
| 95 | + try { |
| 96 | + System.out.print(msg); |
| 97 | + return in.readLine(); |
| 98 | + } catch (IOException e) { |
| 99 | + throw new RuntimeException(e); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Reads a character |
| 105 | + * |
| 106 | + * @return A character containing the value of the input |
| 107 | + * @throws RuntimeException if there is an exception while parsing the character or during input |
| 108 | + */ |
| 109 | + public static char inputChar() { |
| 110 | + try { |
| 111 | + return (char) in.read(); |
| 112 | + } catch (IOException e) { |
| 113 | + throw new RuntimeException(e); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Reads a character after displaying a message |
| 119 | + * |
| 120 | + * @param msg The message to be displayed before reading the input character |
| 121 | + * @return A character containing the value of the input |
| 122 | + * @throws RuntimeException if there is an exception while parsing the character or during input |
| 123 | + */ |
| 124 | + public static char inputChar(String msg) { |
| 125 | + try { |
| 126 | + System.out.print(msg); |
| 127 | + return (char) in.read(); |
| 128 | + } catch (IOException e) { |
| 129 | + throw new RuntimeException(e); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Reads a byte |
| 135 | + * |
| 136 | + * @return A byte containing the value of the input |
| 137 | + * @throws RuntimeException if there is an exception while parsing the number or during input |
| 138 | + */ |
| 139 | + public static byte inputByte() { |
| 140 | + try { |
| 141 | + return Byte.parseByte(in.readLine()); |
| 142 | + } catch (IOException e) { |
| 143 | + throw new RuntimeException(e); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Reads a byte after showing a message |
| 149 | + * |
| 150 | + * @param msg The message to be displayed before reading the input byte |
| 151 | + * @return A byte containing the value of the input |
| 152 | + * @throws RuntimeException if there is an exception while parsing the number or during input |
| 153 | + */ |
| 154 | + public static byte inputByte(String msg) { |
| 155 | + try { |
| 156 | + System.out.print(msg); |
| 157 | + return Byte.parseByte(in.readLine()); |
| 158 | + } catch (IOException e) { |
| 159 | + throw new RuntimeException(e); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Reads a short number |
| 165 | + * |
| 166 | + * @return A short containing the value of the input |
| 167 | + * @throws RuntimeException if there is an exception while parsing the number or during input |
| 168 | + */ |
| 169 | + public static short inputShort() { |
| 170 | + try { |
| 171 | + return Short.parseShort(in.readLine()); |
| 172 | + } catch (IOException e) { |
| 173 | + throw new RuntimeException(e); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Reads a short number after displaying a message |
| 179 | + * @param msg The message to be displayed before reading the input short |
| 180 | + * @return A short containing the value of the input |
| 181 | + * @throws RuntimeException if there is an exception while parsing the number or during input |
| 182 | + */ |
| 183 | + public static short inputShort(String msg) { |
| 184 | + try { |
| 185 | + System.out.print(msg); |
| 186 | + return Short.parseShort(in.readLine()); |
| 187 | + } catch (IOException e) { |
| 188 | + throw new RuntimeException(e); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + |
| 193 | + /** |
| 194 | + * Reads a floating point number |
| 195 | + * @return A float containing the value of the input |
| 196 | + * @throws RuntimeException if there is an exception while parsing the number or during input |
| 197 | + */ |
| 198 | + public static float inputFloat() { |
| 199 | + try { |
| 200 | + return Float.parseFloat(in.readLine()); |
| 201 | + } catch (IOException e) { |
| 202 | + throw new RuntimeException(e); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Reads a floating point number after displaying a message |
| 208 | + * @param msg The message to be displayed before reading the input float |
| 209 | + * @return A float containing the value of the input |
| 210 | + * @throws RuntimeException if there is an exception while parsing the number or during input |
| 211 | + */ |
| 212 | + public static float inputFloat(String msg) { |
| 213 | + try { |
| 214 | + System.out.print(msg); |
| 215 | + return Float.parseFloat(in.readLine()); |
| 216 | + } catch (IOException e) { |
| 217 | + throw new RuntimeException(e); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Reads a floating point number (double) |
| 223 | + * @return A double containing the value of the input |
| 224 | + * @throws RuntimeException if there is an exception while parsing the number or during input |
| 225 | + */ |
| 226 | + public static double inputDouble() { |
| 227 | + try { |
| 228 | + return Double.parseDouble(in.readLine()); |
| 229 | + } catch (IOException e) { |
| 230 | + throw new RuntimeException(e); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * Reads a floating point number (double) after displaying a message |
| 236 | + * @param msg The message to be displayed before reading the input double |
| 237 | + * @return A double containing the value of the input |
| 238 | + * @throws RuntimeException if there is an exception while parsing the number or during input |
| 239 | + */ |
| 240 | + public static double inputDouble(String msg) { |
| 241 | + try { |
| 242 | + System.out.print(msg); |
| 243 | + return Double.parseDouble(in.readLine()); |
| 244 | + } catch (IOException e) { |
| 245 | + throw new RuntimeException(e); |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + /** |
| 250 | + * Reads a long number |
| 251 | + * @return A long containing the value of the input |
| 252 | + * @throws RuntimeException if there is an exception while parsing the number or during input |
| 253 | + */ |
| 254 | + public static long inputLong() { |
| 255 | + try { |
| 256 | + return Long.parseLong(in.readLine()); |
| 257 | + } catch (IOException e) { |
| 258 | + throw new RuntimeException(e); |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + /** |
| 263 | + * Reads a long number after displaying a message |
| 264 | + * @param msg The message to be displayed before reading the input long |
| 265 | + * @return A double containing the value of the input |
| 266 | + * @throws RuntimeException if there is an exception while parsing the number or during input |
| 267 | + */ |
| 268 | + public static long inputLong(String msg) { |
| 269 | + try { |
| 270 | + System.out.print(msg); |
| 271 | + return Long.parseLong(in.readLine()); |
| 272 | + } catch (IOException e) { |
| 273 | + throw new RuntimeException(e); |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + /** |
| 278 | + * Reads a boolean value |
| 279 | + * @return A boolean containing true or false |
| 280 | + * @throws RuntimeException if there is an exception while parsing the value or during input |
| 281 | + */ |
| 282 | + public static boolean inputBoolean() { |
| 283 | + try { |
| 284 | + return Boolean.parseBoolean(in.readLine()); |
| 285 | + } catch (IOException e) { |
| 286 | + throw new RuntimeException(e); |
| 287 | + } |
| 288 | + } |
| 289 | + |
| 290 | + /** |
| 291 | + * Reads a boolean value |
| 292 | + * @param msg The message to be displayed before reading the input boolean |
| 293 | + * @return A boolean containing true or false |
| 294 | + * @throws RuntimeException if there is an exception while parsing the value or during input |
| 295 | + */ |
| 296 | + public static boolean inputBoolean(String msg) { |
| 297 | + try { |
| 298 | + System.out.print(msg); |
| 299 | + return Boolean.parseBoolean(in.readLine()); |
| 300 | + } catch (IOException e) { |
| 301 | + throw new RuntimeException(e); |
| 302 | + } |
| 303 | + } |
| 304 | + |
| 305 | + /** |
| 306 | + * Reads an BigInteger number |
| 307 | + * |
| 308 | + * @return A BigInteger containing the value of the input |
| 309 | + * @throws RuntimeException if there is an exception while parsing the number or during input |
| 310 | + */ |
| 311 | + public static BigInteger inputBigInteger() { |
| 312 | + try { |
| 313 | + return new BigInteger(in.readLine()); |
| 314 | + } catch (IOException e) { |
| 315 | + throw new RuntimeException(e); |
| 316 | + } |
| 317 | + } |
| 318 | + |
| 319 | + /** |
| 320 | + * Reads an BigInteger after showing a message |
| 321 | + * |
| 322 | + * @param msg The message to be displayed before reading the input number |
| 323 | + * @return An BigInteger containing the value of the input |
| 324 | + * @throws RuntimeException if there is an exception while parsing the number or during input |
| 325 | + */ |
| 326 | + public static BigInteger inputBigInteger(String msg) { |
| 327 | + try { |
| 328 | + System.out.print(msg); |
| 329 | + return new BigInteger(in.readLine()); |
| 330 | + } catch (IOException e) { |
| 331 | + throw new RuntimeException(e); |
| 332 | + } |
| 333 | + } |
| 334 | + |
| 335 | + /** |
| 336 | + * Reads an BigDecimal number |
| 337 | + * |
| 338 | + * @return A BigDecimal containing the value of the input |
| 339 | + * @throws RuntimeException if there is an exception while parsing the number or during input |
| 340 | + */ |
| 341 | + public static BigDecimal inputBigDecimal() { |
| 342 | + try { |
| 343 | + return new BigDecimal(in.readLine()); |
| 344 | + } catch (IOException e) { |
| 345 | + throw new RuntimeException(e); |
| 346 | + } |
| 347 | + } |
| 348 | + |
| 349 | + /** |
| 350 | + * Reads an BigDecimal after showing a message |
| 351 | + * |
| 352 | + * @param msg The message to be displayed before reading the input number |
| 353 | + * @return An BigDecimal containing the value of the input |
| 354 | + * @throws RuntimeException if there is an exception while parsing the number or during input |
| 355 | + */ |
| 356 | + public static BigDecimal inputBigDecimal(String msg) { |
| 357 | + try { |
| 358 | + System.out.print(msg); |
| 359 | + return new BigDecimal(in.readLine()); |
| 360 | + } catch (IOException e) { |
| 361 | + throw new RuntimeException(e); |
| 362 | + } |
| 363 | + } |
| 364 | +} |
0 commit comments