0

My aim is to calculate the grade mark for several students who have taken exam for more than one subject. The student records are to be stored within an array. Please find my code below: When printing out student record, I was able to print all records within the outer for loop but unable to print the subjects (within the inner for loop). I am getting error "arrayoutofindex".

package Assignment;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class Assignment {
    // String name;
    int totalMarks;
    String surname;
    String firstname;
    int studNo;
    String subject;

    Assignment(String surname, String firstname, int studNo, String subject) {
        // this.name=name;
        // this.totalMarks=totalMarks;
        this.firstname = firstname;
        this.surname = surname;
        this.studNo = studNo;
        this.subject = subject;
    }

    // public String getName(){
    // return name;
    // }
    public String getSName() {
        return surname;
    }

    public String getFName() {
        return firstname;
    }

    public int getTotalMarks() {
        return totalMarks;
    }

    public int getStudNo() {
        return studNo;
    }

    public String getSubject() {
        return subject;
    }

    public static boolean validSubject(String sub) {
        return sub.matches("[1-5]");
    }

    public static void main(String[] args) {
        ArrayList<Assignment> list = new ArrayList<Assignment>();
        ArrayList<String> l = new ArrayList<String>();
        // boolean validSub = false;
        int times;
        // int numTimes = 1;
        // int sub=0;
        // int times;
        String[] subs;
        String sub = "";
        Scanner input = new Scanner(System.in);
        System.out.print("How many students are in the class?: ");
        int sNo = input.nextInt();
        int count[] = new int[sNo];

        String[] fname = new String[sNo];
        String[] sname = new String[sNo];
        int[] stud_No = new int[sNo];
        // int marks[]=new int[sNo];

        for (int i = 0; i < count.length; i++) {
            System.out.printf("Student%2d:\n", i + 1);
            System.out.print("Student Number: ");
            int s_No = input.nextInt();
            System.out.print("Enter Firstname:");
            String f = input.next();
            System.out.print("Enter Surname:");
            String s = input.next();
            System.out.println("Choose one from the underlisted subjects:");
            System.out.println("Subjects:1. Boat Maintenance");
            System.out.println("\t 2. Basic sail control");
            System.out.println("\t 3. Blue-water Navigation");
            System.out.println("\t 4. Chart reading");
            System.out.println("\t 5. Laws & Customs of the Sea");
            System.out.print("How many subjects will you want to process, Maximum of 3: ");
            int subNo = input.nextInt();
            int[] subj = new int[subNo];
            subs = new String[subNo];

            for (times = 0; times < subj.length; times++) {
                System.out.printf("Subject%2d: ", times + 1);
                // System.out.println("Subject: ");
                sub = input.next();
                subs[times] = sub;
            }

            // }
            // System.out.print("Enter marks in test1:");
            // int t1=input.nextInt();
            // System.out.print("Enter marks in test2:");
            // int t2=input.nextInt();
            // int m=t1+t2;
            fname[i] = f;
            sname[i] = s;
            stud_No[i] = s_No;
            // subs[i] = sub;
            // subj[i] = sub;
            // subj[times] = sub;
            // subj[times] = sub;
            // marks[i]=m;
            list.add(new Assignment(sname[i], fname[i], stud_No[i], subs[times]));
            // subs[times] = sub;

        }
        // int lowest = marks[0];
        // int highest = marks[0];
        // int counter = count[i];
        //
        // for(int i=1; i< marks.length; i++)
        // {
        // if(marks[i] > highest)
        // highest = marks[i];
        // else if (marks[i] < lowest)
        // lowest = marks[i];
        // }
        for (Assignment a : list) {
            // if(a.getTotalMarks()==highest){
            // System.out.println(a.getFName() + " get the highest marks");
            // }
            // if(a.getTotalMarks()==lowest){
            // System.out.println(a.getFName() + " get the lowest marks");
            // }
            System.out.println(a.getFName() + " " + a.getSName());
            System.out.println("Student Number: " + a.getStudNo());
            System.out.println("Subjects: " + a.getSubject());
            System.out.println("=================================");
            // System.out.println(subs[times]);
        }
    }
}

4 Answers 4

0

you have

             subs[times] = sub;
             subj[times]++;

in your inner loop with condition for(times=0;times<subj.length;times++) so i guess that subs is shorter than subj and that whats giving you the exception. Check your conditions and modify it.

0

I guess, subs[times] = sub; should be subj[times] = sub;.

0

I ran you code and the error is

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at Assignment.main(Assignment.java:113)

So what is happening in that line?

list.add(new Assignment(sname[i],fname[i],stud_No[i],subs[times + 1]));

In this way you can trace that the error comes from either sname, fname, stud_No or subs accessing something out of their range. Now you can modify the line and rerun, to figure out what's going on. :)

0

ArrayOutOfBoundException will occur at line
list.add(new Assignment(sname[i],fname[i],stud_No[i],subs[times + 1]));
because times already increased to subj.length which equal to subs.length.So apply if condition here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.