-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathInputLotto.java
More file actions
58 lines (47 loc) · 1.73 KB
/
InputLotto.java
File metadata and controls
58 lines (47 loc) · 1.73 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package lotto.view;
import camp.nextstep.edu.missionutils.Console;
import java.lang.String;
import java.util.ArrayList;
import java.util.List;
public class InputLotto {
// 당첨 번호 입력
public List<Integer> getLotto() {
while(true) { // 예외 발생 시 반복적인 입력을 받기 위한 반복문
try {
System.out.println("Please enter a lotto numbers.");
String strLotto = Console.readLine();
List<Integer> lotto = checkLotto(strLotto);
return lotto;
}
// 숫자가 아니거나 구분자가 이상한 입력
catch (NumberFormatException e) {
System.out.println("[ERROR] It is not a valid lotto.");
}
// 숫자 범위를 벗어나는 입력
catch (IllegalArgumentException e) {
System.out.println("[ERROR] It is out of range.");
}
}
}
// 올바른 입력 체크 함수
public List<Integer> checkLotto(String lotto) {
List<Integer> arrLotto = new ArrayList<Integer>();
// 구분자(,)를 기준으로 문자열 자르기
String[] strLotto = lotto.split(",");
// 문자를 숫자로 변환
for (int i = 0; i < strLotto.length; i++) {
arrLotto.add(Integer.parseInt(strLotto[i]));
}
// 숫자 개수 확인
if (arrLotto.size() != 6) {
throw new IllegalArgumentException();
}
// 숫자 범위 확인
for (int i = 0; i < arrLotto.size(); i++) {
if(arrLotto.get(i) < 1 || arrLotto.get(i) > 45) {
throw new IllegalArgumentException();
}
}
return arrLotto;
}
}