-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathUser_Controller.java
More file actions
42 lines (35 loc) · 1.26 KB
/
Copy pathUser_Controller.java
File metadata and controls
42 lines (35 loc) · 1.26 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
36
37
38
39
40
41
42
package baseball.Controller;
import java.util.HashSet;
import java.util.Set;
public class User_Controller {
public int[] checkUserInput(String input, int size) throws IllegalArgumentException {
checkSize(input, size);
checkSame(input);
return getNum(input, size);
}
private void checkSize(String input, int size) throws IllegalArgumentException {
if (input.length() != size) {
throw new IllegalArgumentException();
}
}
private void checkSame(String input) throws IllegalArgumentException{
Set<Character> charSet = new HashSet<>();
for (int i = 0; i < input.length(); i++) {
char currentChar = input.charAt(i);
if (charSet.contains(currentChar)) {
throw new IllegalArgumentException();
}
charSet.add(currentChar);
}
}
private int[] getNum(String input, int size) throws IllegalArgumentException {
int[] parseInt = new int[size];
for (int i = 0; i < input.length(); i++) {
if (!('0' <= input.charAt(i) && input.charAt(i) <= '9')) {
throw new IllegalArgumentException();
}
parseInt[i] = input.charAt(i) - '0';
}
return parseInt;
}
}