-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInputUtil.java
More file actions
37 lines (31 loc) · 970 Bytes
/
InputUtil.java
File metadata and controls
37 lines (31 loc) · 970 Bytes
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
package cse.school.codejam;
import java.util.Scanner;
public class InputUtil {
private final Scanner scanner;
public InputUtil() {
this.scanner = new Scanner(System.in);
}
public String readString(String prompt) {
System.out.print(prompt);
return scanner.nextLine().trim();
}
public double readDouble(String prompt) {
System.out.print(prompt);
try {
return Double.parseDouble(scanner.nextLine().trim());
} catch (NumberFormatException e) {
System.out.println("Invalid number. Try again.");
return -1.0;
}
}
public int readInt(String prompt) {
while (true) {
System.out.print(prompt);
try {
return Integer.parseInt(scanner.nextLine().trim());
} catch (NumberFormatException e) {
System.out.println("Invalid input. Enter a number.");
}
}
}
}