Onur Olmez
Onur Olmez

Reputation: 37

Java- Getting IndexOutOfBoundsException

I have a problem with where I am getting a IndexOutOfBoundsException on line 22 of my code below. The program's purpose is to act as an expression evaluator with the following order of precedence:

 Operator| Description | Level | Associativity 
 --------+-------------+-------+---------------
    &    | Bitwise AND |   1   | left to right
    ^    | Bitwise XOR |   2   | left to right
    |    | Bitwise OR  |   3   | left to right

If the program is given the input expression like "1 | 8 & 3" using the scanner class, then the program should parse this input and output the result as 1.

Sample output:

Enter the expression: 1 | 8 & 3
Result of the given expression: 1

Explanation:

The way the evaluation is done is:

8 & 3 = 1000 & 0011 = 0000 = 0

1 | 0 = 0001 | 0000 = 1

I have been strugging with getting the program to work properly. Below is my latest code:

import java.util.ArrayList;
import java.util.Scanner;

public class Problem1 {
   public static void main(String[] args) throws Exception {

    try {
        ArrayList<String> inputarray = new ArrayList<String>();
        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);

        System.out.print("Enter an expression with single digit integers (example: \"1 | 8 & 3\"): ");

        if(input.hasNextLine()) {
            inputarray.add(input.nextLine());
        }

        int j = 1;

        j = 1;
        for(int i = 0; i < inputarray.size(); i++) {
            if(inputarray.get(j) == "&") {
                inputarray.add((j+1), Integer.toString(Integer.parseInt(inputarray.get(j-1)) & Integer.parseInt(inputarray.get(j+1))));
                System.out.println(" Result of the given expression: " + inputarray);
                inputarray.remove((j-1));
                inputarray.remove((j));
            }
            if(j <= (inputarray.size() - 3)) {
                j += 2;
            }
            else if(j == (inputarray.size() - 2)) {
                break;
            }
        }

        j = 1;
        for(int i = 0; i < inputarray.size(); i++) {
            if(inputarray.get(j) == "^") {
                inputarray.remove(j);
                inputarray.add(j, Integer.toString(Integer.parseInt(inputarray.get(j-1)) ^ Integer.parseInt(inputarray.get(j+1))));
                inputarray.remove((j-1));
                inputarray.remove((j+1));
            }
            if(j <= (inputarray.size() - 3)) {
                j += 2;
            }
            else if(j == (inputarray.size() - 2)) {
                break;
            }
        }

        j = 1;
        for(int i = 0; i < inputarray.size(); i++) {
            if(inputarray.get(j) == "|") {
                inputarray.remove(j);
                inputarray.add(j, Integer.toString(Integer.parseInt(inputarray.get(j-1)) | Integer.parseInt(inputarray.get(j+1))));
                inputarray.remove((j-1));
                inputarray.remove((j+1));
            }
            if(j <= (inputarray.size() - 3)) {
                j += 2;
            }
            else if(j == (inputarray.size() - 2)) {
                break;
            }
        }

    }
    catch(Exception e) {
        e.printStackTrace();
        System.err.println("Invalid input entered, please try again");
    }
    finally {
        System.out.println();
        main(null);
    }
}
}

Upvotes: 0

Views: 113

Answers (1)

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103155

There are several problems.

  • It appears that you are reading a single String into an ArrayList. That means that the size of the ArrayList is 1. Therefore inputarray.get(1) will yield the exception that you observe. You are probably intending to parse the String character by character but that is not what is happening here.
  • Compare Strings using .equals() rather than ==. This has been discussed many time on Stackoverflow.
  • Adding and removing items to a collection while you are iterating the collection is dangerous and requires special care. Maybe you need to rethink the logic.

Upvotes: 1

Related Questions