Erro... string out of rang:-1

4 respostas
hcbelias

Meu programa esta dando este erro, alguem sabe o que esta acontecendo pq p mim axo que esta tudo correto
o erro esta em : String aux=str.substring (abreChave,fechaChave+1);

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main
{

public Main ()
{
}


public static void main (String[] args)
{
    
    Pattern expression=Pattern.compile ("href");
    String str="abc123";
    
    int ind, abreChave=str.indexOf ("<"), fechaChave=str.indexOf (">"),
            ultimoAbreChave=str.lastIndexOf ("<"), ultimoFechaChave=str.lastIndexOf (">");
    ind=fechaChave;
    String saida="";
    
    while ((ind<=ultimoFechaChave) |(abreChave>=0))
    {
        String aux=str.substring (abreChave,fechaChave+1);
        Matcher matcher;         
        matcher = expression.matcher (aux);
        
        if (matcher.find ()){
            
        }
        else
        {
           aux="<|>";               
        }
        saida=saida.concat (aux);
        abreChave=str.indexOf ("<",ind+1);
        fechaChave=str.indexOf (">",ind+1);
        ind=fechaChave;

        
    }
    System.out.print(saida);
    
    
}

}

4 Respostas

aleck

Vc viu meu código de regex no outro tópico? Eles não faz o que vc quer?

hcbelias

faz, eu refiz utilizando as classes do regex, ficou mais claro o codigo, o problema e o seguinte:
em uma string eu posso ter varias tags…
e com isso tenhu ke fazer a verificacao em todas as tags, eu pego cada tag atraves da linha: String aux=str.substring (abreChave,fechaChave+1);
o problema e que da um erro nesta linha
stringindexoutofbounds : string out of range:-1
eu utilizei o debug, e no exemplo ke eu to usando p teste tenho o seguinte:
abre=5 fecha=13

aleck

Você pode colocar no pattern todas as tags que você deseja buscar, não há necessidade de tanta volta, apenas declare ali todos os padrões que devem ou não ser verificados e deixar o matcher fazer o resto.

[]'s

davidbuzatto

Você está tentando acessar um índice que não existe na String…
O seu abre e fecha estão com valores válidos realmente?

Criado 18 de julho de 2007
Ultima resposta 19 de jul. de 2007
Respostas 4
Participantes 3