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();
}
}
}
2 Answers 2
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.
Comments
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..
if. In case you hit the end element and it satisfiescontainscondition, you will try to accessi+1element which is out of range.java.lang.IndexOutOfBoundsException: Index: 1, Size: 1: The exception is pretty clear. You are trying to access position1of anArrayListwhich has size1, so the only available index is0.