-
Notifications
You must be signed in to change notification settings - Fork 589
Expand file tree
/
Copy pathParser.java
More file actions
36 lines (28 loc) · 1.1 KB
/
Parser.java
File metadata and controls
36 lines (28 loc) · 1.1 KB
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
package baseball.utils;
public class Parser {
public int[] parseUserInput(String input, int size) throws IllegalArgumentException {
checkSize(input, size);
return getParseInt(input, size);
}
private void checkSize(String input, int size) throws IllegalArgumentException {
if (input.length() != size) {
throw new IllegalArgumentException();
}
}
private int[] getParseInt(String input, int size) throws IllegalArgumentException {
int[] parseInt = new int[size];
for (int i = 0; i < input.length(); i++) {
if (!checkDigit(input, i)) {
throw new IllegalArgumentException();
}
parseInt[i] = convertCharToInt(input, i);
}
return parseInt;
}
private int convertCharToInt(String input, int i) {
return input.charAt(i) - '0';
}
private Boolean checkDigit(String input, int i) {
return '0' <= input.charAt(i) && input.charAt(i) <= '9';
}
}