簡體   English   中英

Java:獲取索引越界異常

[英]Java: getting an index out of bounds exception

背景

用 Java 構建匯編器:我正在嘗試將用戶的輸入讀入名為vArrayList 如果用戶輸入與String -array table之一匹配的指令,則將計算相應的操作碼並輸出到文本文件中。

問題

但是,在輸入nop指令並嘗試添加另一條指令后,我得到了index out of bounds 異常

源代碼

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

我得到的錯誤堆棧跟蹤

線程“main”中的異常 java.lang.IndexOutOfBoundsException:

如果你們能提供幫助,將不勝感激。

這個循環永遠不會結束。

while (q)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM