Java: obtenir un index hors des limites exception


Contexte

Construire un assembleur en Java: J'essaie de lire l'entrée de l'utilisateur dans un ArrayList nommé v. Si l'utilisateur entre une instruction qui correspond à l'un des String-array table, alors l'opcodecorrespondant sera calculé et sorti dans un fichier texte.

Problème

Cependant, après avoir entré le nop instruction et en essayant d'ajouter une autre instruction, j'ai obtenu un index hors limites exception.

Code source

//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);

Erreur Stacktrace J'ai obtenu

Exception dans le thread "main" java.lang.IndexOutOfBoundsException:

Si vous pouviez aider, ce serait apprécié.

Author: notescrew, 2019-12-02

1 answers

Cette boucle ne se terminera jamais.

while (q)
 0
Author: Mikred, 2019-12-02 22:55:04