Java: ottenere un indice fuori dai limiti eccezione
Contesto
Creazione di un assemblatore in Java:
Sto cercando di leggere l'input dell'utente in un ArrayList
chiamato v
.
Se l'utente inserisce un'istruzione che corrisponde a una delle String
-array table
, il corrispondenteopcode verrà calcolato e inviato in un file di testo.
Problema
Tuttavia, dopo aver immesso il nop
istruzione e cercando di aggiungere un'altra istruzione, ho ottenuto un indice fuori dai limiti eccezione.
Codice sorgente
//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);
Errore Stacktrace Ho ottenuto
Eccezione nel thread "principale" java.lang.IndexOutOfBoundsException:
Se voi ragazzi poteste aiutare sarebbe apprezzato.
0
1 answers
Questo ciclo non finirà mai.
while (q)
0
Author: Mikred, 2019-12-02 22:55:04