Search code examples
javaarraysobjectexceptionthrow

Updating object even though throw IllegalArgumentException is setup


I am working on a java program where I need to perform tests in my main method against a gradeItem class. In my main method I have an array that is being used to create a new object (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 

In my GradeItem class I have a bunch of exceptions for if the information entered is not what it's supposed to be:

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

My issue is that gradeItem2 is always getting created, even when I change my code from:

     GradeItem gradeItem2 = new GradeItem(int1, a[3], a[4], a[5], a[6], int2, int3);

to:

     GradeItem gradeItem2 = new GradeItem(int1, a[3], a[4], "testing", a[6], int2, int3);

And I can't figure out how I need to change my code so that the exception will throw if the value doesn't match a value in the 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");
 } 

Any help would be appreciated. Thank you!

UPDATE:

I've edited my code to use .equals instead of == but am still not getting the desired outcome. I am trying to have the object gradeItem2 update if the itemType matches anything listed in the itemTypeArray, but if it goes through the loop and there is no match to any of the itemTypeArray values then it would thrown the error. So it's looking like there is an issue with the for loop that I have created.

         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");
     } 

Solution

  • if(itemTypeArray[i] != itemType) { is not the correct way to compare strings. Use !itemTypeArray[i].equals(itemType).

    But even so, you have if(itemTypeArray[i] == itemType) { immediately after, i.e. you are checking a boolean condition and its complement, at least one of which is true, so the throw is unreachable.

    It's unclear what you are actually trying to achieve with that loop. Something involving Arrays.asList(itemTypeArray).contains(itemType) would likely be easier.

    For example, if you are simply trying to assert that the value is in the array, you could use the following instead of the loop:

    if (!Arrays.asList(itemTypeArray).contains(itemType)) {
      throw new IllegalArgumentException(...);
    }
    

    Building the itemTypeArray as a constant Set would likely be a better option, so you don't have to keep building this list; although, it's a tiny list, so the difference is likely negligible.

    An alternative would be to use an enum for the item type.