0

Background

Building an Assembler in Java: I'm trying to read user's input into an ArrayList named v. If the user enters an instruction that matches one of the String-array table, then the corresponding opcode will be calculated and output into a textfile.

Problem

However, after inputting the nop instruction and trying to add another instruction, I got an index out of bounds exception.

Source Code

//Array of instructions 
    String table[] = {"LI", " MALSI", "MAHSI", "MSLSI", "MSHSI", "MALSL", "MAHSL",
        "MSLSL", "MSHSL", "NOP", "A", "AH", "AHS", "AND", "BCW", "CLZ", "MAX", "MIN",
        "MSGN", "MPYU", "OR", "POPCNTH", "ROT", "ROTW", "SHLHI", "SFH", "SFW", "SFHS", "XOR "};
    //Array of binary values of the instructions
    String table2[] = {"0", "10000", "10001", "10010", "10011", "10100", "10101",
        "10110", "10111", "1100000000000000000000000", "1100000001", "1100000010", "1100000011",
        "1100000100", "1100000101", "1100000110", "1100000111", "1100001000", "1100001001",
        "1100001010", "1100001011", "1100001100", "1100001101", "1100001110", "1100001111",
        "1100010000", "1100010001", "1100010010", "1100010011"};
    // TODO code application logic here
    Scanner s = new Scanner(System.in);
    String ins = "";
    String fileName = "outfile.txt";
    System.out.println("Please enter MISP function, enter Q to Quit: ");
    boolean q = true;
    String op = "";
    int c = 0;
    String array[] = new String[64];

    //Loop to keep accepting userinput
    while (q) {
        //accepts the user input 
        ins = s.nextLine();
        //arraylist to hold user input 
        List<String> v = new ArrayList<String>();
        //adds the user input to the arraylist 
        v.add(ins);//user input to nop opcode 
        if (v.get(0).toUpperCase().equals("NOP")) {
            op = "1100000000000000000000000";
        } else if (v.get(1).toUpperCase().equals("LI"))//li opcode 
        {
            String p[] = v[1].split(",", 1);
            op = "0";
            op += toBinary(p[0], 3);
            op += toBinary(p[1], 16);
            op += toBinary(p[2], 5);

Error Stacktrace I got

Exception in thread "main" java.lang.IndexOutOfBoundsException:

If you guys could help it would be appreciated.

4
  • 1
    The stack trace from the exception will tell you the exact line that is failing. Until then we can but guess. I'd guess it's that you're accessing the 3 fields of v[1] without any check that there actually were 3 fields. Commented Dec 2, 2019 at 23:39
  • In your screenshot, the selected line String p[] = v[1].split(",", 1); is referencing the List v like being an array. Never seen that. But agree: you should check for the length of array returned from split call (e.g. if (p.length == 3) etc.). This length can be 0 or more and thus yield to an out-of-bound-exception if randomly access. Commented Dec 3, 2019 at 0:53
  • Same bounds-checking applies before accessing List's elements using List's method size() (e.g. if (v.size() > 2) before accessing second element like v.get(1)). Commented Dec 3, 2019 at 1:03
  • Please insert your stacktrace, like in this example, so we can analyze the issue. Just edit and paste it to the end. Commented Dec 3, 2019 at 1:39

1 Answer 1

0

This loop will never end.

while (q)
Sign up to request clarification or add additional context in comments.

1 Comment

Good observation. But not verified: the loop could end in a controlled way, if inside the OP uses a statement like break; or q = false;. Besides this observation is not an answer, so post observation like this as comment instead :-)

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.