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.