我正在尝试使用此方法读取文件:
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 地址(t1 是将要创建的文件的模式)
之后,我必须读取该人的姓名,保存它,读取地址,保存它并将其写入为他们创建的文件中。
这就是我到目前为止所做的,只是创建不同文件的方法。
希望你能帮助我
谢谢!!
这是 Palabra 类:
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() 没有错,我可以读取一个单词并保存它,对吗?如我错了请纠正我。我试图读取该行中的第一个“单词”,例如“t1”,然后将其与另一个字符数组进行比较,并尝试打印以检查代码是否有效,但它不起作用。我做错了什么??
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,并且您应该确保您没有尝试访问不存在的索引,即这必须是 true nova.llargaria < nova.lletres.length
注意:您正在阅读一行,但您似乎忽略了它。这是故意的吗?
关于java - 读取文件时出现 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35022105/