1

I am a beginner in java programming. I am trying to develop a program but when I ran my program which is posted below and it came back with this error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at Instruction.instructionProcess(Instruction.java:27)
at Instruction.main(Instruction.java:79)
Java Result: 1

Here is my Code:

import java.io.*;
import java.util.*;

public class Instruction {

    public static void firstWord(ArrayList<String> optionCode, ArrayList<String> optionCodeList) {
        for (int i = 0; i<optionCode.size(); i++) {
            if (!optionCodeList.contains(optionCode.get(i))) {
                optionCode.set(i,optionCode.get(i).substring(0, optionCode.get(i).indexOf(' ')) + "  *****ERROR*****");
            }
        }
    }

    public static void instructionProcess(ArrayList<String> optionCode) {
        for (int i=0; i<optionCode.size(); i++) {
            if (optionCode.get(i).contains("LD")) {
                if (optionCode.get(i+1).contains("ADD.D")) {
                    optionCode.add(i+1, "stall   -----------");
                }
            }
        }
        for (int i=0; i<optionCode.size(); i++) {
            if (optionCode.get(i).contains("ADD.D")) {
                if (optionCode.get(i+1).contains("S.D")) {
                    optionCode.add(i+1, "stall   ----------");
                    optionCode.add(i+2, "stall   ----------");
                }
            }
        }
        for (int i=0; i<optionCode.size(); i++) {
            if (optionCode.get(i).contains("BNEZ")) {
                optionCode.add(i+1, "stall   ----------");
            }
            System.out.println(optionCode.get(i));
        }
    }

    public static void printOutput(ArrayList<String> optionCode) {
        for (int i=0; i<optionCode.size(); i++) {
            System.out.println(optionCode.get(i).substring(0, optionCode.get(i).indexOf(' ')));
        }
    }

    public static void main(String[] args) throws IOException {
        ArrayList<String> optionCodeList = new ArrayList<String>();
        optionCodeList.add("LD");
        optionCodeList.add("SD");
        optionCodeList.add("ADD.D");
        optionCodeList.add("SUB.D");
        optionCodeList.add("MUL.D");
        optionCodeList.add("DIV.D");
        optionCodeList.add("SUBI");
        optionCodeList.add("SUB");
        optionCodeList.add("ADDI");
        optionCodeList.add("ADD");
        optionCodeList.add("BNEZ");
        optionCodeList.add("BEZ");
        optionCodeList.add("BRA");

        Scanner File = new Scanner(new File("in_sample.txt"));
        while (File.hasNextLine()) {
            String line = File.nextLine();
            ArrayList<String> optionCode = new ArrayList<>();
            Scanner text = new Scanner(line);
            while (text.hasNextLine()) {
                optionCode.add(text.nextLine());
            }
            firstWord(optionCode, optionCodeList);
            instructionProcess(optionCode);
            printOutput(optionCode);

            PrintWriter outFile = new PrintWriter("Output.txt");
            for (int i = 0; i < optionCode.size(); i++) {
                outFile.println(optionCode.get(i));
            }
            outFile.close();
        }
    }

}
4
  • 1
    Take a look at nested if. In case you hit the end element and it satisfies contains condition, you will try to access i+1 element which is out of range. Commented Mar 26, 2014 at 21:39
  • 4
    java.lang.IndexOutOfBoundsException: Index: 1, Size: 1: The exception is pretty clear. You are trying to access position 1 of an ArrayList which has size 1, so the only available index is 0. Commented Mar 26, 2014 at 21:39
  • You even have line numbers, so you can see exactly which line of code is causing this exception. Commented Mar 26, 2014 at 21:45
  • Also, you have three individual that iterate in the exact same manner. As none of them seem dependent on the previous ones modifying the array, those iterations are really redundant. Commented Mar 26, 2014 at 21:48

2 Answers 2

1

In general look on that lines

 for (int i=0; i<optionCode.size(); i++) {
            if (optionCode.get(i).contains("LD")) {
                if (optionCode.get(i+1).contains("ADD.D")) { // from there you got an exception
                    optionCode.add(i+1, "stall   -----------");

If you would like to get sth from the next element on the list that isn't the elemtent that you current iterates on you have to put weaker condition in loop simply:

 for (int i=0; i<optionCode.size()-1; i++) {
            if (optionCode.get(i).contains("LD")) {
                if (optionCode.get(i+1).contains("ADD.D")) { //now you have sure that i+1 will return sth not null.
                    optionCode.add(i+1, "stall   -----------");
//now you have sure that i+1 will return sth not null.

Please replace your loop condition in every loop that you try to get sth i+1 because it will cause exceptions.

Sign up to request clarification or add additional context in comments.

Comments

1

In

for (int i=0; i<optionCode.size(); i++) {
            if (optionCode.get(i).contains("ADD.D")) {
                if (optionCode.get(i+1).contains("S.D")) {
                    optionCode.add(i+1, "stall   ----------");
                    optionCode.add(i+2, "stall   ----------");
                }
            }
        }

In here when i is maximum optionCode.get(i+1) won't work.. you have to apply exceptions on that scenario..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.