为什么我的程序会抛出异常?

时间:2014-03-29 21:55:59

标签: java arraylist exception-handling

当我尝试为我的学生添加一个新课程时,我必须检查以确保我尝试添加的课时与其他课程时间的学生不冲突,但出于某种原因,代码进入循环以检查存储在数组列表中的其他过程时间,有一个异常,当它们不应该被调用时。例如,我可以将5:00p-10:00p放入一个课程,然后放入1:00p-2:00p进行第二个课程,它会抛出异常,就像没有时会发生冲突一样。请检查注释以查看问题发生的位置。任何想法?

package myschool;

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

public class MySchool {
private static Exception e;

public static void main(String[] args) {

    ArrayList<Student> listStudent = new ArrayList<>();
    ArrayList<Integer> listCourseStart = new ArrayList<>();
    ArrayList<Integer> listCourseEnd = new ArrayList<>();

    boolean continueLoop = true;
    boolean addFirstCourse = true;
    boolean addACourse = false;
    Scanner userInput = new Scanner(System.in);

    int option;

    do{
    try {  
    System.out.println(" What would you like to do?");
    System.out.println(" 1) Add a student");
    System.out.println(" 2) View students");
    System.out.println(" 3) Remove a student");
    System.out.println(" 4) Exit");
    System.out.print("--> ");
    option = userInput.nextInt();

    switch( option ){
            case 1:
                Scanner inputs = new Scanner(System.in);
                String fName, lName;
                int sID;
                double sGPA;

                System.out.print(" First Name:");
                fName = inputs.nextLine();

                System.out.print(" Last Name:");
                lName = inputs.nextLine();

                System.out.print(" ID Number:");
                sID = inputs.nextInt();

                System.out.print(" GPA:");
                sGPA = inputs.nextDouble();

                Student newStudent = new Student(fName, lName, sID, sGPA);
                listStudent.add(newStudent);

                inputs.nextLine();

                while (true) {
                    try {
                        System.out.println("Would you like to add a course? Y/N");
                        String shouldAddCourse = inputs.nextLine();

                        if( "N".equals(shouldAddCourse.toUpperCase()))
                                       break;

                        System.out.print(" CourseName:");
                        String cName = inputs.nextLine();

                        System.out.print(" Instructor:");
                        String instructor = inputs.nextLine();

                        System.out.print(" CourseID:");
                        int cID = inputs.nextInt();

                        System.out.print(" CourseCredit:");
                        int cCred = inputs.nextInt();
                        inputs.nextLine();

                        System.out.print(" StartTime:");
                        String cStart = inputs.nextLine();
                        System.out.print(" AM or PM ?");
                        String startAMorPM = inputs.nextLine();

                        System.out.print(" EndTime:");
                        String cEnd = inputs.nextLine();
                        System.out.print(" AM or PM ?");
                        String endAMorPM = inputs.nextLine();

                        String cStartRemove = cStart.replace(":","");
                        int startInt = Integer.parseInt( cStartRemove );

                        String cEndRemove = cEnd.replace(":","");
                        int endInt = Integer.parseInt( cEndRemove );

                        if( "PM".equals(startAMorPM) || "pm".equals(startAMorPM) || "P".equals(startAMorPM) || "p".equals(startAMorPM) )
                            startInt = startInt + 1200;

                        if( "PM".equals(endAMorPM) || "pm".equals(endAMorPM) || "P".equals(endAMorPM) || "p".equals(endAMorPM) )
                            endInt = endInt + 1200;

                        if( addFirstCourse ){
                            Course newCourse = new Course( cName, instructor, cCred, cStart, cEnd, cID );
                            newStudent.listCourse.add(newCourse);
                            listCourseStart.add( startInt );
                            listCourseEnd.add( endInt );
                            addFirstCourse = false;
                        }else{     

                        for( Integer r: listCourseStart ) {
                            if( startInt >= listCourseStart.get(r) && startInt <= listCourseEnd.get(r) /*|| endInt >= listCourseStart.get(r) && endInt <= listCourseEnd.get(r)*/ ) //the problems happens hear on the first listCourseStart.get(r)
                                throw e;
                            else
                                addACourse = true;
                        }

                        if( addACourse == true ){
                            listCourseStart.add( startInt );
                            listCourseEnd.add( endInt );
                            Course newCourse = new Course( cName, instructor, cCred, cStart, cEnd, cID );
                            newStudent.listCourse.add(newCourse);
                            addACourse = false;
                            }
                        }

                    } catch (Exception e) {
                        System.out.println("You have already added a class at that time!");
                    }
                }
                break;


            case 2:
                if(!listStudent.isEmpty()){
                    for(Student l:listStudent) {
                        System.out.println(l);
                        for(Course n:l.listCourse) {
                            System.out.println(n);
                        }
                        System.out.println();
                    }


                }else
                System.out.println("There are no students to view\n");

               break;

            case 3:
                Scanner removeChoice = new Scanner(System.in);

                try {
                    if(!listStudent.isEmpty()){
                    int j = 0;
                    System.out.println("Which student do you want to remove?");

                        for(Student l:listStudent) {
                            System.out.print(j+1 + ")");
                            System.out.println(l);
                            j++;
                        }
                        int remove = removeChoice.nextInt();
                            listStudent.remove( remove - 1 );
                        System.out.println("Student has been removed\n");
                    }else
                        System.out.println("There are no students to remove\n");

                } catch (Exception e) {
                    System.out.println("There are no students to remove\n");
                }

                break;

            case 4:
                continueLoop = false;
                break;
    }
    } catch (Exception e) {
        System.out.println("That is not a valid option!!!");
        continueLoop = false;
    }

    }while( continueLoop );
}

}

3 个答案:

答案 0 :(得分:0)

你在for循环中抛出异常。

for( Integer r: listCourseStart ) {
    if( startInt >= listCourseStart.get(r) && startInt <= listCourseEnd.get(r) /*|| endInt >= listCourseStart.get(r) && endInt <= listCourseEnd.get(r)*/ ) //the problems happens hear on the first listCourseStart.get(r)
        throw e;// <-- This guy is the culprit, but I'm guessing you already knew that...
    else
        addACourse = true;
}

很难确切地说出它出错的地方(这有点难以阅读你的代码,但它应该按照你的说法行事)。

如果你正在使用IDE,你可能想要使用一些断点,并在输入变量放入列表之前检查输入变量的值,或者让它在命令行上将它们吐出来,之前将它们列入清单。

答案 1 :(得分:0)

我认为问题是你的循环

for( Integer r: listCourseStart ) { // r ist the value in listCourseStart
    // you use the value 'r' as index
    // you have to use for( int i = 0; i.....) or 'r' is the the right value
    if( startInt >= listCourseStart.get(r) && startInt <= listCourseEnd.get(r) ) 
        throw e;
    else
        addACourse = true;
}

试试这个

for( Integer r: listCourseStart ) {
    if( startInt >= r && startInt <= r ) 
        throw e;
    else
        addACourse = true;
}

修改

Yout在你的评论中是正确的。你的病情没有必要。您需要将开始和结束时间存储在一起。这是一个例子:

List<Integer[]> times = new ArrayList<>();
times.add(new Integer[]{900,1100});
times.add(new Integer[]{1300,1400});
for( Integer[] time : times ){
    if( startInt >= time[0] && startInt <= time[1] 
         || endInt >= time[0] && endInt <= time[1] ){
        throw e;
    }
}

一点提示:您的异常e永远不会启动 - 您将获得NullPointerException

答案 2 :(得分:0)

您在for循环中滥用列表listCourseStart

for( Integer r: listCourseStart ) {
  if( startInt >= listCourseStart.get(r) && startInt <= listCourseEnd.get(r) )                                     
    throw e;
  else
    addACourse = true;
}

listCourseStart是一个整数列表,用

for( Integer r: listCourseStart ) {
  // ...
}

迭代列表元素。因此在第一次迭代中r将是第一个列表元素,在第二次迭代中将是第二个列表元素,依此类推。

但是在你的循环中你打电话给listCourseStart.get(r)。列表的get()方法检索给定位置的列表元素。因此,如果第一个列表元素是5,那么使用listCourseStart.get(5),您将得到第五个列表元素。我敢肯定,这不是你想要的。

为什么不使用调试器?您可以逐步运行程序,它会显示实际的变量值,以便您可以详细了解正在进行的操作。