0
Good, I would ask you to help me with this mistake! This is part of a game code, the idea and print something like this
M M M 123456789012345678901 1 2 M positions will vary with each move,
Thank you!
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 19 at java.lang.AbstractStringBuilder.deleteCharAt(Unknown Source) at java.lang.StringBuilder.deleteCharAt(Unknown Source) at JogarBolhao3.imprimeCaminho(JogarBolhao3.java:128) at JogarBolhao3.jogaJogoBolhao3(JogarBolhao3.java:32) at WatchTheBolhao3.main(WatchTheBolhao3.java:40)
In this error dim and 21
public static void imprimeCaminho(int iniMaus [], int dim, int crateras []){
// Imprimir as posicoes dos Maus no Caminho
StringBuilder caminhoPosicoes = new StringBuilder();
for( int i = 0; i < iniMaus.length; i++){
for( int j = 1; j < dim; j++){
if( iniMaus [i] == j){
caminhoPosicoes.deleteCharAt(j); //ERRO APARECE AQUI LINHA 128
caminhoPosicoes.append("M");
}
}
}
// Imprimir o Caminho
StringBuilder caminho = new StringBuilder();
for(int i = 0; i <= dim; i++){
caminho.append(i);
if( i % 10 == 0){
caminho.deleteCharAt(i);
caminho.append("0");
}
for(int r = 0; r <= crateras.length; r++){
if(crateras [r] == i){
caminho.deleteCharAt(i);
}
}
}
// Imprimir as Dezenas no Caminho
StringBuilder caminhoDezenas = new StringBuilder();
for(int i = 0; i <= dim; i++){
if( i % 10 == 0){
int d = (i / 10);
caminhoDezenas.append(d);
}
for(int r = 0; r <= crateras.length; r++){
if(crateras [r] == i){
caminho.deleteCharAt(i);
}
}
}
System.out.println(caminhoPosicoes.toString());
System.out.println(caminho.toString());
System.out.println(caminhoDezenas.toString());
}
Welcome to Sopt. Click [Edit] on the question and let us know in which line the error occurs and if possible, more details than the code should do.
– user28595
Thanks diegofm, I’ve already edited
– Diogo
You create a new Stringbuilder here:
StringBuilder caminhoPosicoes = new StringBuilder();
does not add any content to it and then attempts to access a particular position of it by launching the Outofbounds exception. I believe it is a mistake of logic, but I did not understand what the intention was.– Math