user2647435
user2647435

Reputation: 3

Unable to split the txt file correctly, ArrayIndexOutOfBoundsException

i have a log made by me and i want that my program can search in the log by month. Here is the format of my file.txt:

[31 02/08/13 21:55:47] Name_Surname 0A49G 21

Where the first digit is the week of the year (i managed to get that and i can search by week, and i though it would be the same for the month but it seems i was wrong), and the next 3 numbers are the day/month/year. The problem is that i'm unable to split the array (because netBeans says "Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1"). I marked where netBeans says is the problem. What i want is to get the number of the month so i can do the search.

Here is the code:

    textoMostrado.setText("");
    FileReader fr = null;
    try {
        File file = new File("Registro.txt");
        fr = new FileReader(file);
        if (file.exists()) {
            String line;
            BufferedReader in = new BufferedReader(fr);
            try {
                int mes = Calendar.getInstance().get(Calendar.MONTH);
                mes++;
                int año = Calendar.getInstance().get(Calendar.YEAR);
                año %= 100;
                while ((line = in.readLine()) != null)   {
                    String[] lista = line.split(" ");
                    String [] aux = lista[1].split("/"); //the problem is here
                    int numMes = Integer.parseInt(aux[1]);
                    int numAño = Integer.parseInt(aux[2]);
                    if ((numMes==mes)&&(numAño==año)) {
                        textoMostrado.append(line+"\n"); 
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(MostrarRegistros.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(MostrarRegistros.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            fr.close();
        } catch (IOException ex) {
            Logger.getLogger(MostrarRegistros.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Sorry for my english, is not my native language, i hope someone can help me with this.

Upvotes: 0

Views: 99

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499800

This pair of lines:

String[] lista = line.split(" ");
String [] aux = lista[1].split("/"); //the problem is here

... will fail any time the line doesn't contain a space, because in that case lista will only have one element. You can guard against that:

if (lista.length > 1) {
    String[] aux = lista[1].split("/");
    ...
} else {
    // Whatever you want to do with a line which doesn't include a space.
}

My guess is that your log contains a line which isn't as shown in your sample - you can easily diagnose that just by putting some logging in the else clause above. You may find it's an empty string, by the way...

Upvotes: 5

Related Questions