线程“main”java.lang.IllegalArgumentException中的异常

时间:2013-08-21 20:00:09

标签: java

这是我的错误消息

Exception in thread "main" java.lang.IllegalArgumentException
    at TestGrades.setRaceTime(TestGrade.java:49)
    at GradeDriver.getGradeData(GradeDriver.java:40)
    at Grade Driver.main(GradeDriver.java:26)

Java的类:

import java.util.*;
import java.io.*;

public class GradeDriver{
public static void main(String[] args){
    double[] averageInfo = new double[3];

    Scanner console = new Scanner(System.in);

    String[] fileName = new String[2];

    for(int i = 0; i < args.length; i++){
        fileName[i] = args[i];
    }

    Scanner input = getInputFile(console, fileName);
    PrintStream output = getOutputFile(console, fileName);

    TestGrades[] grades = getGradeData(input);
    showGradeInfo(output, grades);
    getAverageInfo(averageInfo, grades);
    showAverageInfo(output, averageInfo, grades[0]);

}

public static TestGrades[] getGradeData(Scanner input) {
    int numberOfGrades = input.nextInt();
    TestGrades[] grades = new TestGrades[numberOfGrades];

    for(int driver = 0; driver < numberOfGrades && input.hasNext(); driver++) {
        grades[driver] = new TestGrades(input.next(), input.next());
        for (int grade = 1; grade <= 4; grade++) {
            grades[driver].setRaceTime(grade , input.nextDouble());
        }
    }
    return grades;
}

public static void showGradeInfo (PrintStream output, TestGrades[] grades) {
    for (int i = 0; i < grades.length; i++) {
        output.print(( i + 1 ) + ". " + grades[i] + "\n");
    }
}

public static void getAverageInfo (double[] info, TestGrades[] grades) {
    double low = 20.0;
    double high = 0.0;
    double average = 0.0;
    double totalAverage = 0.0;
    for (int i = 0; i < grades.length; i++) {
        average = grades[i].getGradeAverage();
        if (average > high) {
            high = average;
        }
        if (average < low) {
            low = average;
        }
        totalAverage = totalAverage + average;
    }
    info[0] = low;
    info[1] = high;
    info[2] = totalAverage/grades[0].getCounter();
}

public static void showAverageInfo(PrintStream o, double[] info, TestGrades d) {
    String[] s = {"Total number of students: "};

    o.printf("\n%d. %s %d\n", 1, s[0], d.getCounter());
    for (int i = 1; i <info.length; i++) {
        o.printf("%d. %s %.2f/n", i + i, s[i], info[i - 1]);
    }
}
public static Scanner getInputFile(Scanner console, String[] inFileName) {
    boolean fileFound = false;
    Scanner s = null;
    if (inFileName[0] == null) {
        System.out.print("Please enter the input file name: ");
        inFileName[0] = console.nextLine();
    }

    do {
        try {
            s = new Scanner(new File(inFileName[0]));
            fileFound = true;
        }

        catch (FileNotFoundException e) {
            System.out.println("Cannot read file: " + e) ;
            System.out.println("Enter the correct input file name: ");
            inFileName[0] = console.nextLine();
        }
    }
    while (!fileFound);
        return s;
    }

public static PrintStream getOutputFile (Scanner console, String[] outFileName) {
    boolean fileFound = false;
    PrintStream pS = null;
    if (outFileName[1] == null) {
        System.out.print("Please enter output file name: ");
        outFileName[1] = console.nextLine();
    }
    do {
        try {
            pS = new PrintStream (new File (outFileName[1]));
            fileFound = true;
        }
        catch (FileNotFoundException e2) {

            System.out.println("Cannot read file: " + e2);
            System.out.println("Enter a correct output file name: ");
            outFileName[1] = console.nextLine();
        }
    }
    while(!fileFound);
        return pS;
    }
}





import java.io.*;
import java.util.*;

public class TestGrades {
private static int gradeCount = 0;
private String firstName;
private String lastName;
private double[] raceTime = new double[4];

public TestGrades(String fName, String lName) {
    firstName = fName;
    lastName = lName;
    gradeCount++;
}

private String getFirstName() {
    return firstName;

}

private String getLastName() {
    return lastName;

}

public double getGradeAverage() {
    double highest = 0.0;
    double sum = 0.0;
    for (int i = 0; i < raceTime.length; i++) {
        sum += raceTime[i];
        if (raceTime[i] > highest) {
            highest = raceTime[i];
        }
    }
    sum = sum - highest;
    sum = sum / 3;
    return sum;
}

public void setRaceTime(int grade, double time) {
    if (grade < 1 || grade > 4 || time < 0.0 || time > 15.0) {
        throw new IllegalArgumentException();
    }
    raceTime[grade - 1 ] = time;
}

public int getCounter() {
    return gradeCount;
}

public String toString() {
    return (firstName + " " + lastName);
}
} 

真的很感激任何帮助!

2 个答案:

答案 0 :(得分:2)

看起来你自己扔了IllegalArgumentException

if (grade < 1 || grade > 4 || time < 0.0 || time > 15.0) {
    throw new IllegalArgumentException();
}

但是你没有提供消息作为构造函数的参数。您应该提供在显示时更有意义的消息,例如:

throw new IllegalArgumentException("grade (" + grade + ") or time (" + time +
    ") outside of legal range.");

您可能输入超出范围的time值。

答案 1 :(得分:1)

你自己抛出异常......

if (grade < 1 || grade > 4 || time < 0.0 || time > 15.0) {
    throw new IllegalArgumentException();
}