-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleHelper.java
57 lines (31 loc) · 1.09 KB
/
ConsoleHelper.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
package com.codegym.task.task30.task3008;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ConsoleHelper {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static void writeMessage(String message) {
System.out.println(message);
}
public static String readString() {
while (true) {
try {
String data = reader.readLine();
if (data != null) {
return data;
}
} catch (IOException e) {
writeMessage("An error occurred while trying to enter text. Try again.");
}
}
}
public static int readInt() {
while (true) {
try {
return Integer.parseInt(readString().trim());
} catch (NumberFormatException e) {
writeMessage("An error while trying to enter a number. Try again.");
}
}
}
}