读取文件时出现 ArrayIndexOutOfBoundsException

ArrayIndexOutOfBoundsException when reading a file

我正在尝试使用此方法读取文件:

    public static char [] llegir() {
    Palabra nova = new Palabra();
    botarBlancs();
    while ((lletra != fiSequencia) && // No ha acabat la seqüència
            (lletra != blanc)) { // Hi ha prou espai
        nova.lletres[nova.llargaria++] = lletra;
        lletra = leerCarTeclado();
    }
    //System.out.println(nova.girar());
    return nova.lletres;
}

(botarBlancs() 只是跳过空格来区分一个词和另一个词)

这是我使用的代码:

    public static void generador_de_cartas() throws Exception{
    Palabra pl = new Palabra();

    FileReader fr = new FileReader("datos_clientes.txt");
    BufferedReader br = new BufferedReader(fr);
    String line = br.readLine();
    char [] linea;

    while (line != null) {
        linea=line.toCharArray();
        linea=Palabra.llegir();
        System.out.println(linea);
        line=br.readLine();

    }

    br.close();
    fr.close();
}

它在线程 "main" java.lang.ArrayIndexOutOfBoundsException: 20 方法 llegir() 中给了我一个异常,它说:nova.lletres[nova.llargaria++] = lletra ;

我该如何解决这个问题?

关于如何使用该方法读取文件行的​​任何想法?

提前谢谢你

************************编辑********************* **

这是我试过的方法:

    public static void generador_de_cartas() throws Exception {
    Palabra pl = new Palabra();
    char[] tipo1 = {'t', '1'}, tipo2 = {'t', '2'}, tipo3 = {'t', '3'}, nRef = {'#', 'n'}, dRef = {'#', 'd'};
    FileReader fr = new FileReader("datos_clientes.txt");
    BufferedReader br = new BufferedReader(fr);
    String line = br.readLine();
    char[] linea;

    while (line != null) {
        linea = line.toCharArray();
        Palabra.llegir(); //reads line 
        if (Palabra.llegir() == tipo1) {
            //code that creates a new file following the pattern of tipo 1 files
        } else {
            if (Palabra.llegir() == tipo2) {
                //code that creates a new file following the pattern of tipo 2 files
            } else {
                if (Palabra.llegir() == tipo2) {
                    //code that creates a new file following the pattern of tipo 3 files
                }
            }
            line = br.readLine();

        }
        line = br.readLine(); //reads following line
    }

    br.close();
    fr.close();
}

但是一直报同样的异常。

该程序的主要思想在于为主文件 (datos_clientes.txt) 的不同行中表示的每个人创建一个文本文件,文件具有不同的文本或模式,具体取决于人。

主文件的一行如下所示:

t1 #n name #d address(t1是将要创建的文件的模式)

之后,我必须读取此人的姓名,保存,读取地址,保存并将其写入为他们创建的文件中。

这是我到目前为止所做的,只是创建不同文件的方法。

希望你能帮助我

谢谢!!

这是 class 帕拉布拉:

public class Palabra {

public static final char blank = ' ';
public static final char endSequence = '\n';
// Constants privades
// Llargària màxima d'una paraula
private static final int MAXIM = 20;
// ATRIBUTS
private char[] letters;
private int length;
// atribut de classe, per simplificar les operacions de lectura
private static char letter = ' ';
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++EXTRA
private static char[] frase = null;
private static int indice;

// INTERFICIE
// Constructor
public Palabra() {
    letters = new char[MAXIM];
    length = 0;
}

public static char[] llegir() {
    Palabra nova = new Palabra();
    botarBlancs();
    while ((letter != endSequence) && // No ha acabat la seqüència
            (letter != blank)) { // Hi ha prou espai
        if (nova.length < nova.letters.length) {
            nova.letters[nova.length++] = letter;
            letter = leerCarTeclado();

        }
    }

    return nova.letters;
}

public boolean esIgualA(Palabra b) {
    boolean iguals = length == b.length;
    for (int idx = 0; (idx < length) && iguals; idx++) {
        iguals = letters[idx] == b.letters[idx];
    }
    return iguals;
}

public static boolean iguals(Palabra a, Palabra b) {
// using previous esIgualA
    return a.esIgualA(b);
}

public static void botarBlancs() {
    while (letter == blank) {
        letter = leerCarTeclado();
    }
}

static public char leerCarTeclado() {
    char res = '.';
    if (frase != null) {
        res = frase[indice++];
    }
    return res;
}

}

我将一些更改为非静态的,至少我可以 运行 它,现在我想知道我是否正确阅读了该行。如果我的 llegir() 方法没有错,我可以读取一个单词并保存它,对吗?如果我错了纠正我。我试图读取行中的第一个 "word",例如 "t1",然后我将它与另一个 char 数组进行比较,并尝试打印以检查代码是否有效,但它没有't。我做错了什么??

        while (line != null) {
        linea = line.toCharArray();
        char [] tipo=Palabra.llegir(); //reads first word 

        if (tipo == tipo1) {
           System.out.print("tipo1") ;//code that creates a new file following the pattern of tipo 1 files
        } else {
            if (tipo == tipo2) {
                System.out.print("tipo2") ;//code that creates a new file following the pattern of tipo 2 files
            } else {
                if (tipo == tipo2) {
                   System.out.print("tipo3") ; //code that creates a new file following the pattern of tipo 3 files
                }
            }


        }
        line = br.readLine(); //reads following line
    }

您应该在每次调用该方法时从数组的开头开始,即 0,并且您应该确保您没有尝试访问不存在的索引,即这必须为真 nova.llargaria < nova.lletres.length

注意:您正在阅读一行,但您似乎忽略了它。这是故意的吗?

您没有提供足够的信息...但是:

在调试器中打开它(例如,Intellij IDEA 有一个很棒的调试器)。如果您通过单击左边距并在其上 right-click 创建断点,您可以告诉它在什么条件下停止在断点处。

告诉它在nova.llargaria is >= nova.lletres.length时停在断点处。这将让您在崩溃之前看到发生了什么。

此外,您可以只检查代码中的 nova.llargaria is >= nova.lletres.lengthcontinue; 以跳过它,这样它就不会在这种情况下崩溃。