java.lang.StringIndexOutOfBoundsException: String index out of range: - 1

0

I have the following error when wanting to read content from a .txt file and compare it with the contents of an ArrayList which I do:

public void leer(ArrayList<String> arrayList) {
        try {
            // Abrimos el archivo con la ruta especificada.
            FileInputStream fstream = new FileInputStream(new File("ruta.txt"));
            // Creamos el objeto de entrada
            DataInputStream entrada = new DataInputStream(fstream);
            // Creamos el Buffer de Lectura
            BufferedReader buffer = new BufferedReader(new InputStreamReader(entrada));
            String contenido;
            // Leer el archivo linea por linea
            while ((contenido = buffer.readLine()) != null) {

                  String[] arrayStrings = contenido.split(",");
                  String[] array = arrayList.toArray(new String[arrayList.size()]);
                for (int i = 0; i < arrayStrings.length - 1; i++) {
                    for (int j = i + 1; j < arrayList.size(); j++) {
                        if (arrayStrings[i].substring(0, arrayStrings[i].lastIndexOf(",")).equals(array[j].substring(0, array[j].lastIndexOf(",")))) {
                            System.out.println("Contedodo: "+arrayStrings[i]+"     Contenido array: "+array[j]);
                        }
                    }
                }
//                 Imprimimos la línea por pantalla
//                System.out.println(contenido);
            }
            // Cerramos el archivo
            entrada.close();
        } catch (Exception e) { //Catch de excepciones
            System.err.println("Ocurrio un error: " + e.getMessage());
        }
    }

The content of the .txt file is as follows:

25, SAN SALVADOR

45, SAN MIGUEL

32, SAN JERONIMO

20, BUENOS AIRES

The content of the array is:

PDA25_20171022164520

PDA45_20171022164520

PDA35_20171022164520

PDA55_20171022164520

Where I want to look for the two-digit code of the .txt file and compare it with the one in the array but I can not do it. The first problem I have is the Exception java.lang.StringIndexOutOfBoundsException: String index out of range:-1 and the other problem is that I do not know how to compare them, that is, I look for the code in the .txt file and compare it with the one inside the array, showing which province corresponds each code, I explain .. any ej that help me? Thanks again.

    
asked by Gerardo Ferreyra 30.08.2017 в 04:01
source

1 answer

0

Your code may look like this

public void leer(ArrayList<String> arrayList) {

    Map<String, String> mapaCodigosProvincia = new HashMap();

    try {
        // Abrimos el archivo con la ruta especificada.
        FileInputStream fstream = new FileInputStream(new File("ruta.txt"));
        // Creamos el objeto de entrada
        DataInputStream entrada = new DataInputStream(fstream);
        // Creamos el Buffer de Lectura
        BufferedReader buffer = new BufferedReader(new InputStreamReader(entrada));
        String contenido;
        // Leer el archivo linea por linea

        while ((contenido = buffer.readLine()) != null) {
            if(!contenido.equals("")){
                String[] arrayStrings = contenido.split(",");
                mapaCodigosProvincia.put(arrayStrings[0], arrayStrings[1]);
            }else{
                continue;
            }
        }

        // Cerramos el archivo
        entrada.close();
    } catch (Exception e) { //Catch de excepciones
        System.err.println("Ocurrio un error: " + e.getMessage());
    }

    //Recorremos el arrayList
    for(String nombreArchivo : arrayList){
        String[] separador = nombreArchivo.split("_");
        String provincia = separador[0].replace("PDA", "").trim();

        //Obtenemos el nombre de la provincia
        String nombreProvincia = mapaCodigosProvincia.get(provincia);
    }

}
    
answered by 30.08.2017 / 06:18
source