java.lang.ArrayIndexOutOfBoundsException: -1 - Labirinto Java

5 respostas Resolvido
java
O

Sempre que saio do alcance da matriz ele dá esse erro, ja tentei criar condições para quando tentar sair o programa parar, porém não funcionou

Segue código:
´´´

/*Dupla: Giovanni Ota

*Ra: 819148633
*SIN1BN-MCA
*Vitor Vendramini
*
*SIN1BN-MCA
*/

package labirinto;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;
import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Lab {

public static void main(String[] args) {
	
	int mapa[][] = {
			{1,1,1,1,1,1,1,1,1},
			{1,1,1,0,0,0,0,1,1},
			{1,1,1,0,1,1,1,0,1},
			{1,0,0,2,0,0,0,0,1},
			{1,0,1,0,1,1,1,1,1},
			{0,0,1,0,0,0,0,0,1},
			{1,1,1,1,1,1,1,1,1}
	};
	
	JFrame tela = new JFrame();
	tela.setTitle("Labirinto - Giovanni Ota, Vitor Vendramini - SIN1BN-MCA");
	tela.setSize(500,500);
	tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	JPanel painel = new JPanel();
	painel.setLayout(new FlowLayout());
	painel.setBackground(new Color(254,200,60));
	
	JLabel rotulos [][] = new JLabel [7][9];
	
	for(int i = 0; i < 7; i++) {
		for (int j = 0; j < 9; j++) {
			rotulos[i][j] = new JLabel();
			if (mapa[i][j] == 1) {
				rotulos[i][j].setText("X ");
			}
			if (rotulos[i][j].getText() == "X ") {
				rotulos[i][j].setForeground(new Color(211, 25, 28));
			}
			if (mapa[i][j] == 0) {
				rotulos[i][j].setText("   ");
			}
			if (mapa[i][j] == 2) {
				rotulos[i][j].setText("H ");
			}
			if (rotulos[i][j].getText() == "H ") {
				rotulos[i][j].setForeground(new Color(51, 51, 51));
			}
		}
	}
	
	for(int i = 0; i < 7; i++) {
		for (int j = 0; j < 9; j++) {
			rotulos[i][j].setFont(new Font("Arial", Font.BOLD, 50));
			painel.add(rotulos[i][j]);
		}
	}
	
	JTextField caixa = new JTextField();
	painel.add(caixa);
	caixa.setSize(10,20);
	caixa.addKeyListener(new KeyListener() {
		
		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void keyPressed(KeyEvent e) {
			
			if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				for(int i = 0; i < 7; i++) {
					for(int j = 0; j < 9; j++) {
						if (rotulos[i][j].getText() == "H " &&  rotulos[i][j - 1].getText() != "X ") {
							rotulos[i][j].setText("   ");
							
							rotulos[i][j - 1].setText("H ");
						}
					}
				}
			}
			
			if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				for(int i = 0; i < 7; i++) {
					for(int j = 0; j < 9; j++) {
						if (rotulos[i][j].getText() == "H " && rotulos[i][j + 1].getText() != "X ") {
							rotulos[i][j].setText("   ");
							rotulos[i][j + 1].setText("H ");
							break;
						}
					}
				}
			}
			
			if (e.getKeyCode() == KeyEvent.VK_UP) {
				for(int i = 0; i < 7; i++) {
					for(int j = 0; j < 9; j++) {
						if (rotulos[i][j].getText() == "H " && rotulos[i - 1][j].getText() != "X ") {
							rotulos[i][j].setText("   ");
							rotulos[i - 1][j].setText("H ");
							
						}
					}
				}
			}
			
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				for(int i = 0; i < 7; i++) {
					for(int j = 0; j < 9; j++) {
						if (rotulos[i][j].getText() == "H " && rotulos[i  + 1][j].getText() != "X ") {
							rotulos[i][j].setText("   ");
							rotulos[i += 1][j].setText("H ");
							
						}
					}
				}
			}
			
			if (rotulos[5][7].getText() == "H " || rotulos[2][7].getText() == "H " || rotulos[1][6].getText() == "H ") {
				Erro error = new Erro();
				error.setVisible(true);
			}
			if (rotulos[5][0].getText() == "H ") {
				Certo out = new Certo();
				out.setVisible(true);
			}
		}
	});
	
	tela.setContentPane(painel);
	tela.setVisible(true);
}

}
´´´

5 Respostas

darlan_machado

Sim, está tentando acessar uma posição que não existe no array/matriz.

O

E como eu posso limitar para não sair da matriz? Já tentei várias condições

darlan_machado

Quais?

O
Todos dentro do evento das teclas:

if (j - 1 == -1){

continue

}

Ja tentei no if da tecla esquerda colocar no for j < 9 && j >=0

if (rotulos[i][j].getText() == “”){

continue;

}

 não tenho mais ideia, pq o labirinto e a movimentação ta funcionando normal mas como é para entregar esse trabalho queria tirar esse erro
O
Solucao aceita
Consegui arrumar colocando o método de andar dentro de um try catch, ficou assim:

´´´

try {

if (e.getKeyCode() == KeyEvent.VK_LEFT) {

for(int i = 0; i < 7; i++) {

for(int j = 0; j < 9; j++) {

if (rotulos[i][j].getText() == "H " &&  rotulos[i][j - 1].getText() != "X “) {

rotulos[i][j].setText(”   ");				

rotulos[i][j - 1].setText("H ");

}

}

}

}

} catch (Exception x) {
}

´´´

Criado 4 de dezembro de 2019
Ultima resposta 4 de dez. de 2019
Respostas 5
Participantes 2