본문 바로가기
오류리포트

[오류리포트] Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 at class02.Error01.main(Error01.java:127)

by ssunooo 2024. 7. 5.

 

 

Resident03에 상속된 Cat03,Frog03을 생성한뒤,

메인에 배열을 이용하여 datas의 값을

4개를 입력하는 과정에서 오류가 났다.

 

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
91
92
93
94
95
96
97
class Resident03 { // class 주민 생성
    String type="주민"// 무조건 있음
    String name; // 주민을 생성할때, 이름을 반드시 설정하면서 만들어야함
    
    Resident03(String name) { // 이름만 설정할 경우 "주민"
        this("주민",name);
        System.out.println(this.toString());
    }
    Resident03(String type,String name) { // 타입, 이름 설정
        this.type=type;
        this.name=name;
        System.out.println(this.toString());
    }
    
    void hello() { // 행복함 / 무난함 / 슬픔 / 화남
        Random random=new Random();
        int feel=random.nextInt(4); // 이 4개중에서 랜덤으로, 0~3까지
        if(feel==0) { // 출력할거야~, 행복해
            System.out.print("행복해^^ ");
            this.sound(); // 울음소리 추가출력
        }
        else if(feel==1) {
            System.out.print("무난해~ ");
            this.sound();
        }
        else if(feel==2) {
            System.out.print("슬퍼ㅠㅠ ");
            this.sound();
        }
        else {
            System.out.print("화나!!! ");
            this.sound();
        }
    }
    void action(String tool) { // 도구를 썼을때
        if(tool=="잠자리채") { // 잠자리채를 썼을때
            System.out.println("곤충채집~");
        }
        else if(tool=="낚시대") { // 낚시대를 썼을때
            System.out.println("생선낚시~");
        }
        else if(tool=="삽") { // 삽을 썼을때
            System.out.println("땅파기~");
        }
        else { // 그 외의 도구를 입력했을때
            System.out.println("그 도구는 없어요ㅠ");
        }
    }
    void sound() { // 울음소리 추가출력
        System.out.println("주민~");
    }
    @Override
    public String toString() {
        return "내이름은 "+this.name+"(이)야";
    }
    
}
 
class Cat03 extends Resident03 { // Resident class에 상속받은 Cat
    Cat03(String name) { // 이름을 반드시 설정해야함
        super("고양이",name);
    }
 
    @Override
    void sound() { // 울음소리 오버라이딩 고양이 라면 야옹
        System.out.println("야옹~");
    }
}
 
class Frog03 extends Resident03 { // Resident class에 상속받은 Frog
    Frog03(String name) { // 이름을 반드시 설정해야함
        super("개구리",name);
    }
    
    @Override
    void sound() { // 울음소리 오버라이딩 개구리 라면 개굴
        System.out.println("개굴~");
    }
}
 
public class Error01 {
    public static void main(String[] args) {
        
        
        
        Resident03[] datas=new Resident03[3]; // 주민 4명만 받을거야~
        datas[0]=new Cat03("히죽");
        datas[0].hello();
        datas[0].action("잠자리채");
        datas[1]=new Cat03("1호");
        datas[2]=new Frog03("레이니");
        datas[2].hello();
        datas[2].action("삽");
        datas[3]=new Frog03("아이다");
            
    }
}
cs

 

배열의 생성자를 3개를 만든다고 해놓고

4개를 만들어서 생긴 오류로 확인되어서

Resident03[] datas=new Resident03[3]에서 Resident[4]로 변경해주니

오류가 해결되었다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Error01 {
    public static void main(String[] args) {
        
        
        
        Resident03[] datas=new Resident03[4]; // 주민 4명만 받을거야~
        datas[0]=new Cat03("히죽");
        datas[0].hello();
        datas[0].action("잠자리채");
        datas[1]=new Cat03("1호");
        datas[2]=new Frog03("레이니");
        datas[2].hello();
        datas[2].action("삽");
        datas[3]=new Frog03("아이다");
    }
}
cs

 

 

 

2024.07.05