-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScanner.java
90 lines (80 loc) · 2.09 KB
/
Scanner.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
81
82
83
84
85
86
87
88
89
90
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Scanner {
private static final Pattern num = Pattern.compile("\\-?\\d+");
private final BufferedInputStream in;
private int c;
public Scanner(InputStream stream) {
in = new BufferedInputStream(stream);
try {
c = (char)in.read();
} catch (IOException e) {
c = -1;
}
}
public boolean hasNext() {
return c != -1;
}
public String next() {
StringBuilder sb = new StringBuilder();
try {
while (c <= ' ') {
c = in.read();
}
while (c > ' ') {
sb.append((char)c);
c = in.read();
}
} catch (IOException e) {
c = -1;
return "";
}
return sb.toString();
}
public String nextLine() {
StringBuilder sb = new StringBuilder();
try {
while (c != '\n' && c != -1) {
sb.append((char)c);
c = in.read();
}
if (c != -1)
c = in.read();
} catch (IOException e) {
c = -1;
return "";
}
return sb.toString();
}
public int nextInt() {
String s = next();
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) {
return 0; //throw new Error("Malformed number " + s);
}
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public void useLocale(int l) {}
/**
* reads integer numbers separated by non-digit at the current line
* only at one line
* @return
*/
public int[] readInts() {
Matcher matcher = num.matcher(nextLine());
List<Integer> result = new ArrayList<>();
while (matcher.find()) {
result.add(Integer.valueOf(matcher.group()));
}
return result.stream().mapToInt(i->i).toArray();
}
}