首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >即使设置了抛出IllegalArgumentException,也要更新对象

即使设置了抛出IllegalArgumentException,也要更新对象
EN

Stack Overflow用户
提问于 2018-09-24 02:12:15
回答 1查看 41关注 0票数 0

我正在开发一个java程序,我需要在我的main方法中针对gradeItem类执行测试。在我的main方法中,我有一个用于创建新对象(GradeItem2)的数组:

代码语言:javascript
复制
// 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类中,如果输入的信息不是预期的那样,我有一大堆异常:

代码语言:javascript
复制
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总是会被创建,即使我将代码从:

代码语言:javascript
复制
     GradeItem gradeItem2 = new GradeItem(int1, a[3], a[4], a[5], a[6], int2, int3);

至:

代码语言:javascript
复制
     GradeItem gradeItem2 = new GradeItem(int1, a[3], a[4], "testing", a[6], int2, int3);

而且我不知道如何更改代码,以便在值与itemTypeArray中的值不匹配时抛出异常:

代码语言:javascript
复制
 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而不是==,但是仍然没有得到想要的结果。我尝试让对象gradeItem2在itemType与itemTypeArray中列出的任何内容匹配的情况下进行更新,但如果它遍历循环,但与任何itemTypeArray值都不匹配,那么它将抛出错误。所以看起来我创建的for循环有问题。

代码语言:javascript
复制
         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");
     } 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-24 02:23:58

if(itemTypeArray[i] != itemType) {不是比较字符串的正确方法。使用!itemTypeArray[i].equals(itemType)

但即便如此,你在后面紧跟着if(itemTypeArray[i] == itemType) {,也就是说你正在检查一个布尔条件及其补码,其中至少有一个是真的,所以抛出是无法到达的。

现在还不清楚你到底想通过这个循环来达到什么目的。涉及Arrays.asList(itemTypeArray).contains(itemType)的事情可能会更容易一些。

例如,如果您只是试图断言该值在数组中,则可以使用以下代码而不是循环:

代码语言:javascript
复制
if (!Arrays.asList(itemTypeArray).contains(itemType)) {
  throw new IllegalArgumentException(...);
}

itemTypeArray构建为一个常量集可能是一个更好的选择,因此您不必一直构建这个列表;尽管它是一个很小的列表,因此差异可能可以忽略不计。

另一种方法是对项目类型使用枚举。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52468941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档