【发布时间】:2019-02-27 08:26:47
【问题描述】:
我正在开发一个 java 程序,我需要在我的 main 方法中针对 GradeItem 类执行测试。在我的主要方法中,我有一个用于创建新对象(gradeItem2)的数组:
// Uses string array from input file to create new gradeItem object
public static void processGradeItemData(String[] a) {
int int1 = Integer.parseInt(a[2]);
int int2 = Integer.parseInt(a[7]);
int int3 = Integer.parseInt(a[8]);
System.out.println("Running Test 2b:");
if (a[1].equals("ADD")) {
GradeItem gradeItem2 = new GradeItem(int1, a[3], a[4], a[5], a[6], int2, int3);
System.out.println(gradeItem2.toString() + "\n");
}
} // End processGradeItemData
在我的 GradeItem 类中,如果输入的信息不是应有的信息,我有很多例外情况:
public class GradeItem {
private int gradeItemId; // Unique ID for each item graded
private String studentId; // Unique ID for each Student
private String courseId; // Unique ID for each course
private String itemType; // Item Type validated with array
private String date; // Date the item was due
private int maximumScore; // Maximum points that can be earned
private int actualScore; // Score of item recieved by student
String[] itemTypeArray = new String[]{"HW", "Quiz", "ClassWork", "Test",
"Final"};
//************************************************************************
GradeItem() {
} // end GradeItem
//************************************************************************
public GradeItem(int gradeItemId, String studentId, String courseId,
String itemType, String date, int maximumScore,
int actualScore) {
if (gradeItemId < 0) {
throw new IllegalArgumentException("Grade item id cannot be blank");
}
if (studentId.equals("")) {
throw new IllegalArgumentException("Student id cannot be blank");
}
if (courseId.equals("")) {
throw new IllegalArgumentException("Course id cannot be blank");
}
if (itemType.equals("")) {
throw new IllegalArgumentException("Item type cannot be blank");
}
for(int i = 0; i <= 4; i++) {
if(itemTypeArray[i] != itemType) {
continue;
}
if(itemTypeArray[i] == itemType) {
break;
}
throw new IllegalArgumentException("Item type must be valid selection");
}
if (date.equals("")) {
throw new IllegalArgumentException("Date cannot be blank");
}
if (maximumScore < 0) {
throw new IllegalArgumentException("Maximum score must be greater"
+ "than 0");
}
if (actualScore < 0 || actualScore > maximumScore) {
throw new IllegalArgumentException("Actual score must be between"
+ "0 and " + maximumScore);
}
this.gradeItemId = gradeItemId;
this.studentId = studentId;
this.courseId = courseId;
this.itemType = itemType;
this.date = date;
this.maximumScore = maximumScore;
this.actualScore = actualScore;
} // end GradeItem
} // end GradeItem
我的问题是,gradeItem2 总是被创建,即使我从以下位置更改代码:
GradeItem gradeItem2 = new GradeItem(int1, a[3], a[4], a[5], a[6], int2, int3);
到:
GradeItem gradeItem2 = new GradeItem(int1, a[3], a[4], "testing", a[6], int2, int3);
而且我不知道如何更改我的代码,以便在值与 itemTypeArray 中的值不匹配时抛出异常:
for(int i = 0; i <= 4; i++) {
if(itemTypeArray[i] != itemType) {
continue;
}
if(itemTypeArray[i] == itemType) {
break;
}
throw new IllegalArgumentException("Item type must be valid selection");
}
任何帮助将不胜感激。谢谢!
更新:
我已将代码编辑为使用 .equals 而不是 ==,但仍然没有得到预期的结果。如果 itemType 与 itemTypeArray 中列出的任何内容匹配,我试图让对象gradeItem2 更新,但如果它通过循环并且与任何 itemTypeArray 值都不匹配,那么它将引发错误。所以看起来我创建的 for 循环存在问题。
for(int i = 0; i <= 4; i++) {
if(!itemTypeArray[i].equals(itemType)) {
continue;
}
else
if(itemTypeArray[i].equals(itemType)) {
break;
}
throw new IllegalArgumentException("Item type must be valid selection");
}
【问题讨论】:
-
有趣的问题。我还没有分析所有内容...您能否包含一个包含所有内容的minimal reproducible example 测试程序,以便我可以一次复制所有内容?
标签: java arrays object exception throw