I'm not able to identify the error in my code, could someone tell me what's wrong, have tried everything but can not find that this can be Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4.
I'm Brazilian, my variables will get a little complicated for you to understand, because I would not have edited a lot of work to change all the code
package Aulas_POO;
import javax.swing.JOptionPane;
public class edicoes {
static byte espaço=10;
static String pesquisa;
static String informacoes[][] = new String [10][4];
static String escolhaMenu;
public static void main(String[] args) {
String opcao="";
do{
opcao= ExibirMenu();
if(opcao.equals("1")){ Cadastro(); }
if(opcao.equals("2")){ pesquisa(); }
if(opcao.equals("3")){ edicao(); }
if(opcao.equals("4")){visualizar(); }
if(opcao.equals("5")){deletarTudo();}
if(opcao.equals("6")){deletarUm(); }
}while(!opcao.equals("7"));
}
private static String ExibirMenu() {
// TODO Auto-generated method stub
String menu ="DIGITE UMA DAS OPÇÕES\n\n\n"
+"1) Cadastro\n"
+"2) Pesquisa de nomes\n"
+"3) Edição de nomes\n"
+"4) Visualizar a lista\n"
+"5) Deletar todos cadastros\n"
+"6) Deletar Apenas um cadastro\n"
+"7) SAIR";
return JOptionPane.showInputDialog(menu);
}
private static void deletarUm() {
// TODO Auto-generated method stub
pesquisa=JOptionPane.showInputDialog("Informe o Cpf");
for (int i = 0; i < informacoes.length; i++) {
if (pesquisa.equals(informacoes[i][1])){
informacoes[i][0]=null;
informacoes[i][1]=null;
informacoes[i][2]=null;
informacoes[i][3]=null;
}
}
JOptionPane.showMessageDialog(null,"Cadastro Deletado com SUCESSO!");
}
private static void deletarTudo() {
// TODO Auto-generated method stub
for (int i = 0; i < informacoes.length; i++) {
informacoes[i][0]=null;
informacoes[i][1]=null;
informacoes[i][2]=null;
informacoes[i][3]=null;
}
}
private static void visualizar() {
// TODO Auto-generated method stub
String exibeInformacoes="";
for (int i = 0; i < informacoes.length; i++) {
if(!(informacoes[i][i]==null)){
exibeInformacoes+="\nNome: "+informacoes[i][0]+", CPF: "+informacoes[i][1]+", CEP: "+informacoes[i][2]+" e Telefone: "+informacoes[i][3];
}
}
JOptionPane.showMessageDialog(null,exibeInformacoes);
}
private static void edicao() {
// TODO Auto-generated method stub
pesquisa=JOptionPane.showInputDialog("Informe o CPF");
escolhaMenu="Digite \n\n"+
"1-Editar nome\n"+
"2-Editar cep\n"+
"3-Editar telefone\n"+
"4-Editar todos\n"+
"5-Sair";
for (int i = 0; i < informacoes.length; i++) {
if(escolhaMenu.equals("1")){
informacoes[i][0]=JOptionPane.showInputDialog("Informe o novo Nome");}
if(escolhaMenu.equals("2")){
informacoes[i][2]=JOptionPane.showInputDialog("Informe o novo cep");}
if(escolhaMenu.equals("3")){
informacoes[i][3]=JOptionPane.showInputDialog("Informe o novo telefone");}
if(escolhaMenu.equals("4")){
informacoes[i][0]=JOptionPane.showInputDialog("Informe o novo Nome");
informacoes[i][1]=JOptionPane.showInputDialog("Informe o novo cpf");
informacoes[i][2]=JOptionPane.showInputDialog("Informe o novo cep");
informacoes[i][3]=JOptionPane.showInputDialog("Informe o novo telefone");}
if(escolhaMenu.equals("5")){return;}
}
}
private static void pesquisa() {
// TODO Auto-generated method stub
pesquisa=JOptionPane.showInputDialog("Informe o CPF");
escolhaMenu="Digite \n\n"+
"1-Pesquisar nome\n"+
"2-Pesquisar cep\n"+
"3-Pesquisar telefone\n"+
"4-Pesquisar todos\n"+
"5-Sair";
for (int i = 0; i < informacoes.length; i++) {
if(pesquisa.equals(informacoes[i])){
if(escolhaMenu.equals("1")){
JOptionPane.showMessageDialog(null,informacoes[i][0]);}
}
if(escolhaMenu.equals("2")){
JOptionPane.showMessageDialog(null,informacoes[i][2]);}
if(escolhaMenu.equals("3")){
JOptionPane.showMessageDialog(null,informacoes[i][3]);}
if(escolhaMenu.equals("4")){
JOptionPane.showMessageDialog(null,"\n"+informacoes[i][i]);}
}
if(escolhaMenu.equals("5")){
return;
}
}
private static void Cadastro() {
// TODO Auto-generated method stub
if(espaço!=0){
informacoes[0][0]=JOptionPane.showInputDialog("Informe o nome");
informacoes[0][1]=JOptionPane.showInputDialog("Informe o cpf");
informacoes[0][2]=JOptionPane.showInputDialog("Informe o cep");
informacoes[0][3]=JOptionPane.showInputDialog("Informe o telefone");
JOptionPane.showMessageDialog(null,"Cadastro efetuado com SUCESSO!");
espaço--;
}
else{
JOptionPane.showMessageDialog(null,"Não há mais espaço disponível , remova algum cadastro para novo cadastro");
}
}
}
For future reference, please cut down your code to just a short but complete example which demonstrates the problem, and if you're reporting an exception, include the stack trace and relevant line numbers.
I suspect that this is the problem:
for (int i = 0; i < informacoes.length; i++) {
if(!(informacoes[i][i]==null)){
That's going to cause a problem when i
is 4.
That may not be the only problem of course - your code may not have reached that far. It's relatively hard to read over 240 lines of code without any explanation of what you're trying to achieve, and an inadequate description of what's going wrong.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.