-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsole.java
80 lines (71 loc) · 2.05 KB
/
Console.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
68
69
70
71
72
73
74
75
76
77
78
79
80
package my.yahorfilipchyk.console;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Yahor Filipchyk
* Implementing of simple custom console
*/
public class Console {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
/**
* Writes string to stdout.
* @param str String to be written to stdout.
*/
public static void write(String str) {
System.out.print(str);
}
/**
* Writes line to stdout.
* @param str String to be written to stdout.
*/
public static void writeLine(String str) {
System.out.println(str);
}
/**
* Read one line from stdin.
* @return String read from stdin.
*/
public static String readLine() {
String line = "";
try {
line = reader.readLine();
} catch (IOException ex) {
Logger.getLogger(Console.class.getName()).log(Level.SEVERE, null, ex);
}
return line;
}
/**
* Reads input data after the message.
* @param message String to be written to stdout before the input.
* @return String read from stdin after the printed message.
*/
public static String read(String message) {
write(message);
String line;
try {
line = reader.readLine();
String parts[] = line.split(message);
return parts[0];
} catch (IOException ex) {
Logger.getLogger(Console.class.getName()).log(Level.SEVERE, null, ex);
}
return "";
}
/**
* Reads one character from stdin.
* @return Code of read character. -1 if nothing read.
*/
public static int read() {
int readed = -1;
try {
readed = reader.read();
} catch (IOException ex) {
Logger.getLogger(Console.class.getName()).log(Level.SEVERE, null, ex);
}
return readed;
}
}