Java 使用FileReader、ArrayIndexOutOfBounds的奇怪数组情况

Java 使用FileReader、ArrayIndexOutOfBounds的奇怪数组情况,java,arrays,Java,Arrays,我想放弃,但我必须这样做,所以你是我最后的希望。我想这是个容易的问题,但我看不出有什么问题。代码如下: int i = -1; String[][] dan = new String[20][13]; try { FileReader odczytanie = new FileReader("Kontrahenci.txt"); BufferedReader bufor = new BufferedReader(odczytanie); St

我想放弃,但我必须这样做,所以你是我最后的希望。我想这是个容易的问题,但我看不出有什么问题。代码如下:

    int i = -1;
    String[][] dan = new String[20][13];
    try {
     FileReader odczytanie = new FileReader("Kontrahenci.txt");
     BufferedReader bufor = new BufferedReader(odczytanie);
     String str;
     str = bufor.readLine();
     System.out.println(str);
     while ((str = bufor.readLine()) != null) {
        i = i + 1;
        String[] ar = {null, null, null, null, null, null, null, null, null, null, null, null, null};
        ar=str.split("; ");
        for(int j = 0; j < 13; j++)
            dan[i][j] = ar[j];
        for(int j = 0; j < 13; j++)
            System.out.println(dan[i][j]);  
     }
     bufor.close();
    } catch (IOException e) {
           System.out.println("File Read Error");
        }
对于这一行:

for(int j = 0; j < 13; j++)
    System.out.println(ar[j]);

在一个文件中,我有三行用分号分隔的单词。这段代码在第一行非常有效,但在我收到错误后。不知道出了什么问题。

硬编码13似乎有点可疑。为ar设置的值将被要拆分的赋值覆盖。ar不再一定有13个字段。那怎么办

for (int j = 0; j < ar.length ; j++)
这些数据一定是格式错误,或者不是您所期望的。尝试确定正确的大小或打印它

正如Jerry所指出的,固定大小的阵列是一种糟糕的做法。您最好使用一些更高级别的集合,如哈希映射列表或其他自动增长的集合

一个权宜之计是在使用i进行索引之前检查i是否小于20,然后抛出异常或断言。这将为你指明正确的方向。您也可以将其添加到while循环中,但这可能会导致数据未读

String[] ar = {null, null, null, null, null, null, null, null, null, null, null, null, null};
ar=str.split("; ");
带有13个空值的ar声明将立即被str.split;,因此,您不能假设数组的大小总是13。与使用13作为for循环的上限不同,我建议使用ar.lenor或尝试为每个循环使用一个

我怀疑你的dan数组也会有相同的IndexOutOfBoundsError


您应该避免使用幻数。

您应该在创建数组之前计算出文件中有多少行,或者切换到允许更改大小的内容:

ArrayList<String[]> dan = new  ArrayList<String[]>();
try
{
    BufferedReader bufor = new BufferedReader(new FileReader("Kontrahenci.txt"));
    while (bufor.ready())
    {
        dan.add(bufor.readLine().split("; "));
    }
    bufor.close();
}
catch (IOException e)
{
    System.out.println("File Read Error");
}

for(String[] ar : dan)
{
    for(String s : ar)
    {
        System.out.println(s);
    }
}

我没有更改您的错误处理,但请注意,如果出现异常,您不会调用close。

您的代码中有神奇的数字20、13。将这些替换为常量。您提到的两行代码在上述代码中不可用…将调试器设置在引发并检查ar的内容和大小以及失败的j值的行上。如果文件的行数超过20行,我将在dan之外建立索引。如果一行少于12个分号,j将在ar之外索引。如果一行多于12个分号,j将在dan之外索引。@home哦,是的,我的错,对不起。它应该是forint j=0;j<13;j++dan[i][j]=ar[j];这将解决ar的问题,但不会解决dan的问题。非常感谢!我有两个错误,一个是13,第二个是文本文件。我花了大约两个小时思考,而你让它变得如此迅速,呃;]
String[] ar = {null, null, null, null, null, null, null, null, null, null, null, null, null};
ar=str.split("; ");
ArrayList<String[]> dan = new  ArrayList<String[]>();
try
{
    BufferedReader bufor = new BufferedReader(new FileReader("Kontrahenci.txt"));
    while (bufor.ready())
    {
        dan.add(bufor.readLine().split("; "));
    }
    bufor.close();
}
catch (IOException e)
{
    System.out.println("File Read Error");
}

for(String[] ar : dan)
{
    for(String s : ar)
    {
        System.out.println(s);
    }
}