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.

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
Mzhu3703
  • 1
  • 1
  • 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. –  Dec 02 '19 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. – hc_dev Dec 03 '19 at 00: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)`). – hc_dev Dec 03 '19 at 01:03
  • Please __insert your stacktrace__, like in this [example](https://stackoverflow.com/questions/15462649/index-out-of-bounds-exception-java), so we can analyze the issue. Just [edit] and paste it to the end. – hc_dev Dec 03 '19 at 01:39

1 Answers1

0

This loop will never end.

while (q)
Mikred
  • 98
  • 6
  • 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 :-) – hc_dev Dec 03 '19 at 01:21