我正在开发一个java程序,我需要在我的main方法中对gradeItem类执行测试。在我的main方法中,我有一个用于创建新对象的数组(gradeItem2):
[Java] 纯文本查看 复制代码 // 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类中,如果输入的信息不是它应该是的那样,我有一堆例外:
[Java] 纯文本查看 复制代码 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:
[Java] 纯文本查看 复制代码 GradeItem gradeItem2 = new GradeItem(int1, a[3], a[4], a[5], a[6], int2, int3);
至
[Java] 纯文本查看 复制代码 GradeItem gradeItem2 = new GradeItem(int1, a[3], a[4], "testing", a[6], int2, int3);
我无法弄清楚我需要如何更改代码,以便在值与itemTypeArray中的值不匹配时抛出异常:
[Java] 纯文本查看 复制代码 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循环存在问题。
[Java] 纯文本查看 复制代码 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");
}
|