Question

I am working on a program for my class that takes arguments from the command line (java A D C B etc.) and compares them to a character array. In this case it is an key for an exam and the command line arguments are answers. I think I have it almost figured out but have been running into an arrayIndexOutofBoundsException: 1 for a while. I am not very familiar with command line arguments so I may be missing something important but this information is not covered in my java book. Any advice would be appreciated. Thanks in advance.

Code:

public class GradeExam {
  /** Main method */
  public static void main(String args[]) {


    // Key to the questions
    char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};

    int correctCount = 0;

      for(int i = 0; i < args.length; i++){

        String newargs = args[i];

        char[] argarray = newargs.toCharArray();

        for (int j = 0; j < args.length; j++) {
             if (argarray[j] == keys[j])
                 correctCount++;
        }

      }

      System.out.println("Your correct count is " +
              correctCount);

  }
}


Input: java GradeExam D B D C C D A E A D

Expected output: Your correct count is 10
Was it helpful?

Solution 2

You get arrayIndexOutofBoundsException: 1 on this line:

if (argarray[j] == keys[j])

because your argarray there has always only one element, and j takes values from 0...args.length.

Also, you don't need an inner loop at all, you could simplify your program like this:

public static void main(String args[]) {
    char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};
    int correctCount = 0;
    for (int i = 0; i < args.length && i < keys.length; i++) {
        if (args[i].charAt(0) == keys[i]) {
            correctCount++;
        }
    }
    System.out.println("Your correct count is " + correctCount);
}

OTHER TIPS

If you call the program like this...

java GradeExam D B D C C D A E A D

...then each letter is a separate argument (args[0] = "D", args[1] = "B", etc). Based on your code, I think you meant to do this:

java GradeExam DBDCCDAEAD

You also probably want your inner loop to look like this:

for (int j = 0; j < argarray.length && j < keys.length; j++) {
    if (argarray[j] == keys[j])
        correctCount++;
}

Then you won't run off the end of either array, even if the command line argument has too many or too little letters.

The issue is:

 char[] argarray = newargs.toCharArray();

    for (int j = 0; j < args.length; j++) {
         if (argarray[j] == keys[j])
             correctCount++;
    }

You're essentially trying to convert your String argument that you're currently looking at into a character array, when really you should just convert your String argument in a single character. Once you've converted it into a character, you can compare that against the current key you're looking at.

You might want to do some some check in the beginning to be sure that the number of answers given is equal to the number of keys as shown below.

public class GradeExam {

public static void main(String[] args) {

    char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};

    if (args.length != keys.length) {
        throw new RuntimeException("Expected number of input answers: " + keys.length + " but got " + args.length);
    }

    int correctCount = 0;
    try {
        for (int i = 0; i < keys.length; i++) {
            char key = keys[i];
            // We need to convert the String into a character
            char answerChar = args[i].charAt(0);
            if (answerChar == key) {
                correctCount++;
            }
        }
    } catch (IndexOutOfBoundsException ex) {
        ex.printStackTrace();
    }

    System.out.println("Your correct count is " + correctCount);

}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top