如何在java中使用嵌套循环从数组中读取值

时间:2014-02-15 21:31:48

标签: java arrays

我的目标是计算几个考试过多个科目的学生的成绩标记。学生记录将存储在数组中。 请在下面找到我的代码: 打印出学生记录时,我能够打印外部for循环中的所有记录,但无法打印主题(在内部for循环内)。我收到错误“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 个答案:

答案 0 :(得分:0)

你有

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

在您的内部循环中,条件为for(times=0;times<subj.length;times++),所以我猜subssubj更短,而且是什么给了您例外。检查您的条件并进行修改。

答案 1 :(得分:0)

我想,subs[times] = sub;应为subj[times] = sub;

答案 2 :(得分:0)

我运行了代码,错误是

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

那条线上发生了什么?

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

通过这种方式,您可以跟踪错误来自sname,fname,stud_No或subs访问其范围之外的内容。 现在您可以修改该行并重新运行,以找出正在发生的事情。 :)

答案 3 :(得分:0)

ArrayOutOfBoundException 将出现在第
一行 list.add(new Assignment(sname[i],fname[i],stud_No[i],subs[times + 1]));
因为时间已经增加到subj.length,等于subs.length.So适用于条件这里。