我的代码中发生了意外的ArraysIndexOutOfBounds异常

时间:2018-12-05 13:09:35

标签: java arrays

此Java程序基本上用于管理n个学生的班级。它具有各种功能,例如出勤等,但是在编译时会出现ArraysIndexOutOfBounds异常,这是谁的原因,我无法计算。请帮忙。

import java.util.*;

class CLManager
{

    static Scanner ss = new Scanner(System.in);
    static Scanner dd = new Scanner(System.in);
    static Scanner ii = new Scanner(System.in);
    static Scanner cc = new Scanner(System.in);


    int i, j, k, OnRoll;
    String PRemark[] = {"Good Job !", "Excellent !", "Well Done !", "Phenomenal !", "Amazing Performance !", "Keep it up !"};
    String NRemark[] = {"Can do better !", "More effort needed !", "Work harder !", "Needs practice !", "Keep Trying !", "Don't lose hope !"};


    CLManager()
    {
        i=0;
        j=0;
        k=0;

        System.out.println("Enter total no. of students in class");
        OnRoll = ii.nextInt();
    }

    int marks[]  = new int[OnRoll];
    int rollno[] = new int[OnRoll];
    int ranks[]  = new int[OnRoll];

    String names[]   = new String[OnRoll];
    String remarks[] = new String[OnRoll];
    String grades[]  = new String[OnRoll];

    CLManager(int OnRoll)
    {
        System.out.println("Enter names of students in class");
        for(i=0; i<OnRoll; i++)
        {
            names[i] = ss.nextLine(); //error occurs here
            rollno[i] = (i+1);
        }
    }


    void Attendance()
    {
        int P=0, A=0;
        char rec;
        System.out.println("Enter P for present and A for absent");

        for(i=0; i<OnRoll; i++)
        {
            System.out.println(names[i]);
            rec = cc.next().charAt(0);
            if(rec=='P'||rec=='p')
            {
                P++;
            }
            else
            {
                A++;
            }
            System.out.println();
        }
        System.out.println("Present :\t"+P);
        System.out.println("Absent  :\t"+A);
        System.out.println("On Roll :\t"+OnRoll);
    }


    void GradeMarksRemarks()
    {
        System.out.println("Enter marks/100");
        for(i=0; i<OnRoll; i++)
        {
            marks[i] = ii.nextInt();
        }

        for(i=0; i<OnRoll; i++)
        { 
            if(marks[i]<=100&&marks[i]>=90)
            {
                grades[i]="A+";
                remarks[i]="Highly Commendable";
            }
            else if(marks[i]<=89&&marks[i]>=80)
            {
                grades[i]="A";
                remarks[i]="Commendable";
            }
            else if(marks[i]<=79&&marks[i]>=70)
            {
                grades[i]="B+";
                remarks[i]="Very Good";
            }
            else if(marks[i]<=69&&marks[i]>=60)
            {
                grades[i]="B";
                remarks[i]="Good";
            }
            else if(marks[i]<=59&&marks[i]>=50)
            {
                grades[i]="C+";
                remarks[i]="Average";
            }
            else if(marks[i]<=49&&marks[i]>=40)
            {
                grades[i]="C";
                remarks[i]="Borderline";
            }
            else if(marks[i]<=39&&marks[i]>=35)
            {
                grades[i]="D+";
                remarks[i]="Below Average";
            }
            else if(marks[i]<=35)
            {
                grades[i]="D";
                remarks[i]="Fail";
            }
        }
    }


    void GenerateRanks()
    {
        int a=0, b=0 ,m=0 , n=0;
        String p="", q="", x="", y="";

        for(i=0; i<OnRoll; i++)
        {
            for(j=0; j<OnRoll-1-i; j++)
            {
                if(marks[j]>marks[j+1])
                {
                    marks[j] = a;
                    marks[j+1] = b;
                    marks[j] = b;
                    marks[j+1] = a;
                    a=0; b=0;

                    rollno[j] = m;
                    rollno[j+1] = n;
                    rollno[j] = n;
                    rollno[j+1] = m;
                    m=0; n=0;

                    names[j] = p;
                    names[j+1] = q;
                    names[j] = p;
                    names[j+1] = q;
                    p=""; q="";

                    grades[j] = x;
                    grades[j+1] = y;
                    grades[j] = y;
                    grades[j+1] = x;
                    x=""; y="";
                }
            }
        }

        for(i=0; i<OnRoll; i++)
        {
            ranks[i] = (i+1); 
        }
    }


    void DisplayRanks()
    {
        System.out.println("Rank\t\tName\t\tGrade");
        for(i=0; i<OnRoll; i++)
        {
            System.out.print(ranks[i]+"\t\t"+names[i]+"\t\t"+grades[i]);
            System.out.println();
        }
    }


    void DisplayReport()
    {
        System.out.println("Enter roll no.");
        int roll = ii.nextInt();
        System.out.println();

        System.out.println("Rank\t\tName\t\tGrade");
            System.out.print(roll+"\t\t"+names[roll]+"\t\t"+ranks[roll]+"\t\t"+grades[roll]);
        if(marks[roll]>=65)
        {
            System.out.println(PRemark[(int)((Math.random())*6)]);
        }
        else
        {
            System.out.println(NRemark[(int)((Math.random())*6)]);
        }
    }


    public static void main(String args[])
    {
        CLManager fn = new CLManager();
        System.out.println("Welcome to Class Manager!");



        inf :
        while(true)
        {
            System.out.println("For Attendance, press A");
            System.out.println("For Grades and Marks, press M");
            System.out.println("For generation, press G\nFor Positions, press P");
            System.out.println("For Report, press R\nFor Exit, press E");
            System.out.println();
            char ch = cc.next().charAt(0);

            switch(ch)
            {
                case 'E' :
                    System.out.println("TERMINATED");
                    break inf;

                case 'A' :
                    fn.Attendance();
                    break;

                case 'M' :
                    fn.GradeMarksRemarks();
                    break;

                case 'G' :
                    fn.GenerateRanks();
                    break;

                case 'P' :
                    fn.DisplayRanks();
                    break;

                case 'R' :
                    fn.DisplayReport();
                    break;

                default : System.out.println("INVALID INPUT");
            }
        }
        System.out.println("Thanks for using ! :):)");
    }
}

我不明白为什么问题首先出现。我发生了一些错误吗?该代码对我来说似乎没问题,但是由于某种原因,每当我开始输入名称,甚至是数组名称中的字母[]时,我都会收到错误

3 个答案:

答案 0 :(得分:0)

您遇到此错误,因为您的names数组使用OnRoll等于0的默认值初始化,因为您没有提供它。

您需要将此初始化放入您的构造函数中

marks[]  = new int[OnRoll];
rollno[] = new int[OnRoll];
ranks[]  = new int[OnRoll];

names[]   = new String[OnRoll];
remarks[] = new String[OnRoll];
grades[]  = new String[OnRoll];

答案 1 :(得分:0)

您收到此错误,是因为当程序进入第二个构造器OnRoll时仍未未初始化,因此请进行此修改

    CLManager(int OnRoll)
    {

        this.OnRoll = OnRoll;  // add only this line of code
        System.out.println("Enter names of students in class");
        for(i=0; i<OnRoll; i++)
        {
            names[i] = ss.nextLine(); //error occurs here -> resolved
            rollno[i] = (i+1);
        }
    }

答案 2 :(得分:0)

由于出现第二个构造函数从未在输入所有名称的位置调用过而出现此错误。

您应该这样做

CLManager()
{
    i=0;
    j=0;
    k=0;

    System.out.println("Enter total no. of students in class");
    OnRoll = ii.nextInt();
    initializeAllArraysAndCollectNames(OnRoll);
}

int marks[];
int rollno[];
int ranks[];

String names[];
String remarks[];
String grades[];

void initializeAllArraysAndCollectNames(int OnRoll)
{
    marks  = new int[OnRoll];
    rollno = new int[OnRoll];
    ranks  = new int[OnRoll];
    names   = new String[OnRoll];
    remarks = new String[OnRoll];
    grades  = new String[OnRoll];
    System.out.println("Enter names of students in class");
    for(i=0; i<OnRoll; i++)
    {
        names[i] = ss.nextLine(); //error occurs here
        rollno[i] = (i+1);
    }
}

在这里,输入OnRoll后,数组将初始化并接受名称输入。

您可以在此处找到完整的代码https://ideone.com/Lrb7yZ