Delfino
Delfino

Reputation: 1009

Command Line Input Issues (Java)

I have a program that reads in a specific grammar for a CFL and outputs the separate productions for the start symbol, but for some reason passing in command line arguments doesn't work.

I've tried using a String[] in the program, named input and using that seems to provide output, but when I try and run the program with specific commands from the command line, I get errors.

Here's the code:

    char[][] prod; int prodCount = 0, numProds = 0;
    String[] input = new String[] {"a","a","S","b","X",
                                   "a","a","S","a","X",
                                   "a","S","a","X"};

    for(int i = 0; i < input.length; i++) {
        if(input[i] == "X") 
            prodCount++;
    }

    if(input[input.length - 1] == "X") {
        prod = new char[prodCount + 1][1];
        prod[prod.length - 1][0] = '\0';
    } else
        prod = new char[prodCount][];

    int current = 0;
    for(int i = 0; i < input.length; i++) {
        if(input[i] == "X") {
            prod[current] = new char[numProds];
            current++; numProds = 0;
        } else
            numProds++;
    }

    int currentTerminal = 0; current = 0;
    for(int i = 0; i < input.length; i++) {
        if(input[i] == "X") {
            currentTerminal = 0;
            current++;
        }
        else {
            prod[current][currentTerminal] = input[i].charAt(0);
            currentTerminal++;
        }
    }

    for(int i = 0; i < prod.length; i++) {
        for(int j = 0; j < prod[i].length; j++) {
            if(prod[i][j] == '\0')
                System.out.print("E");
            else
                System.out.print(prod[i][j]);
        }
        System.out.println();
    }

Using this code with the String[] input gives me

aaSb
aaSa
aSa
E

However, when I try using input from the command line I get...

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
             Line 36

Here's the line that the above error points to

prod[current][currentTerminal] = args[i].charAt(0);

I'm not sure what's different between String[] input and String[] args. Can someone let me know where I'm making a mistake?

Upvotes: 0

Views: 125

Answers (1)

user3896255
user3896255

Reputation:

Your index is out of bounds here: for(int j = 0; j < prod[i].length; j++) {

Because prod is a new, NULL, array, as defined here:

  if(input[input.length - 1] == "X") { //This is the problem line
    prod = new char[prodCount + 1][1];
    prod[prod.length - 1][0] = '\0';
} else
    prod = new char[prodCount][];

You can't compare a String using ==. Since this comparison will always evaluate to FALSE (in this particular snippet), you're declaring prod with this line: prod = new char[prodCount][]; every time. Notice, that nowhere else in your code do you alter this array or assign anything to it. Thus, the out of bounds you receive later when you attempt to access indexes that don't exist.

Change your code to to use String::equals() like this:

  if(input[input.length - 1].equals("X") { //Here's the fix
    prod = new char[prodCount + 1][1];
    prod[prod.length - 1][0] = '\0';
} else
    prod = new char[prodCount][];

Edit:

I should clarify that you need to make this change throughout your code, because you use the == operator several times, not just in the snippet above.

Upvotes: 1

Related Questions