Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法正确拆分txt文件,ArrayIndexOutOfBoundsException_Java - Fatal编程技术网

Java 无法正确拆分txt文件,ArrayIndexOutOfBoundsException

Java 无法正确拆分txt文件,ArrayIndexOutOfBoundsException,java,Java,我有一个由我做的日志,我希望我的程序可以在日志中按月搜索。 以下是my file.txt的格式: [31 02/08/13 21:55:47]姓名/姓氏0A49G 21 其中第一个数字是一年中的一周(我设法得到了这个数字,我可以按周搜索,我认为这个月的数字是一样的,但似乎我错了),接下来的3个数字是天/月/年。 问题是我无法拆分数组(因为netBeans说“线程中的异常”AWT-EventQueue-0“java.lang.ArrayIndexOutOfBoundsException:1”)。我

我有一个由我做的日志,我希望我的程序可以在日志中按月搜索。 以下是my file.txt的格式:

[31 02/08/13 21:55:47]姓名/姓氏0A49G 21

其中第一个数字是一年中的一周(我设法得到了这个数字,我可以按周搜索,我认为这个月的数字是一样的,但似乎我错了),接下来的3个数字是天/月/年。 问题是我无法拆分数组(因为netBeans说“线程中的异常”AWT-EventQueue-0“java.lang.ArrayIndexOutOfBoundsException:1”)。我标记了netBeans所说的问题所在。我想要的是得到一个月的号码,这样我就可以进行搜索

代码如下:

    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);
        }
    }
对不起,我的英语不是我的母语,我希望有人能帮我解决这个问题。

这两行:

String[] lista = line.split(" ");
String [] aux = lista[1].split("/"); //the problem is here
。。。任何时候该行不包含空格时都将失败,因为在这种情况下,
lista
将只有一个元素。你可以注意:

if (lista.length > 1) {
    String[] aux = lista[1].split("/");
    ...
} else {
    // Whatever you want to do with a line which doesn't include a space.
}
我的猜测是,您的日志中包含一行与示例中所示不同的内容—您只需在上面的
else
子句中添加一些日志记录,就可以轻松地诊断出这一点。顺便说一下,您可能会发现它是一个空字符串…

这两行:

String[] lista = line.split(" ");
String [] aux = lista[1].split("/"); //the problem is here
。。。任何时候该行不包含空格时都将失败,因为在这种情况下,
lista
将只有一个元素。你可以注意:

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

我的猜测是,您的日志中包含一行与示例中所示不同的内容—您只需在上面的
else
子句中添加一些日志记录,就可以轻松地诊断出这一点。您可能会发现它是一个空字符串,顺便问一下……

您是否使用调试器停止并查看该行的
lista
内容?您可能正在读取与模式不匹配的行(可能是注释或空行)。您是否使用调试器停止并查看该行的
lista
内容?有可能你读的某行内容与模式不符(可能是注释或空行)。非常感谢,这就解决了我的问题。我认为lista应该类似于lista[0]=[31;lista[1]=02/08/13;等等。这就是为什么我想用一个辅助符号来分隔。我不确定为什么lista只有一个元素,正如你所说的,这是因为我没有看到一个空行?@user2647435:是的,
lista
将是这样的-但只适用于有空格的行。是的,空行可以解释它-但我们无法判断你是否有空行r文件实际上有一个。非常感谢,这解决了我的问题。我认为lista应该类似于lista[0]=[31;lista[1]=02/08/13;等等。这就是为什么我想用一个辅助符号来分隔。我不确定为什么lista只有一个元素,正如你所说的,这是因为我没有看到一个空行?@user2647435:是的,
lista
将是这样的-但只适用于有空格的行。是的,空行可以解释它-但我们无法判断你是否有空行r文件实际上有一个。