Click here to Skip to main content
15,886,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem with my main class when I build my project..

I get message :

[성적처리 프로그램]
몇명의 학생들의 data를 입력하시겠습니까?
2
1번째 학생
이름을 입력하세요 :
chos
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at HW02_2.addinfo(HW02_2.java:18)
at HW02_2.main(HW02_2.java:95)


Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;


public class HW02_2 {
	public static void addinfo(int num, int[][] array) throws IOException{ // InputStream 객체에 리더를 연결
		int student_num = num;
		for(int i = 0; i < num ; i++){
		//get name info
			int intname = 0;
			InputStreamReader s = new InputStreamReader(System.in);
			BufferedReader in = new BufferedReader(s);
			 
			System.out.println(i+1+"th student\n enter name : ");
		    intname = s.read();
		    array[i][0] = intname;
        // codenumber는?[1]
            System.out.println("enter code number : ");
            Scanner codenum = new Scanner(System.in);
            array[i][1] = codenum.nextInt();
            codenum.close();
        //korean score[2]
            System.out.println("enter korean score : ");
            Scanner korscore = new Scanner(System.in);
            array[i][2] = korscore.nextInt();
            korscore.close();
        //English score[3]
            System.out.println("enter english score : ");
            Scanner engscore = new Scanner(System.in);
            array[i][3] = engscore.nextInt();
            engscore.close();
        //math score[4]
            System.out.println("enter math score : ");
            Scanner mathscore = new Scanner(System.in);
            array[i][4] = mathscore.nextInt();
            mathscore.close();
		}
		show(student_num, array);
	}
	static void show(int num, int[][] array){
		for(int i = 0; i < num ; i++){
			calculate(i, array);
		}
		// sorting by average
		int temp[] = null;
		for(int i = 0; i < num-1; i++){
			for(int j = i+1; j < num; j++){
				if(array[i][6] < array[j][6]){
					temp = array[i];
					array[i] = array[j];
					array[j] = temp;
				}
			}
		}
		// print result
		System.out.println("[Result]");
		for(int i = 0; i < num; i++){
			char grade = 'F';
			if(array[i][6] >= 90){
				grade = 'A';
			}else if(array[i][6] >= 80 && array[i][6] <= 89){
				grade = 'B';
			}else if(array[i][6] >= 70 && array[i][6] <= 79){
				grade = 'C';
			}else if(array[i][6] >= 60 && array[i][6] <= 69){
				grade = 'D';
			}else if(array[i][6] < 60){
				grade = 'F';
			}
			
			System.out.print((char)array[i][0]+""+array[i][1]+" "+array[i][2]+" "
			+array[i][3]+" "+array[i][4]+" "+array[i][5]+" "+array[i][6]+" "+grade+" "+i+1);
		}
	}
	static void calculate(int i, int[][]array){
		//total[5]
		for(int j = 2; j == 4; j++){
			array[i][5] += array[i][j];
		}
		//average[6]
		array[i][6] = array[i][5] / 3;
	}
	public static void main(String[] args) throws IOException {
		int[][] studentinfo = {};
		int students_num = 0;
		
			System.out.println("[성적처리 프로그램]\n몇명의 학생들의 data를 입력하시겠습니까?"); 
			Scanner s = new Scanner(System.in);
			students_num = s.nextInt();
			
			addinfo(students_num, studentinfo);
			s.close();
			
		
	}
}
Posted
Comments
Richard MacCutchan 11-Apr-15 8:54am    
You forgot to allocate any space for your studentinfo array.

So look at array and see what sizes it is declared as.
Either there are not enough elements in the "x" dimension so "i" is overflowing it, or there are less than 7 in the "y" dimension: array[x][y].

We can't do that for you - so use the debugger and look at what is happening!
 
Share this answer
 
You may want to change this part
C#
public static void main(String[] args) throws IOException {
        int[][] studentinfo = {};
        int students_num = 0;

            System.out.println("[성적처리 프로그램]\n몇명의 학생들의 data를 입력하시겠습니까?");
            Scanner s = new Scanner(System.in);
            students_num = s.nextInt();

            addinfo(students_num, studentinfo);
            s.close();


    }


As

C#
public static void main(String[] args) throws IOException {
        int students_num = 0;

            System.out.println("[성적처리 프로그램]\n몇명의 학생들의 data를 입력하시겠습니까?");
            Scanner s = new Scanner(System.in);
            students_num = s.nextInt();


            int[][] studentinfo = new int[students_num][7];


            addinfo(students_num, studentinfo);
            s.close();


    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900