I'm trying to read a file with this method:
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() simply jumps blank spaces to distinguish one word from another)
Here's the code I'm using:
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();
}
And it gives me a Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20 at method llegir() where it says: nova.lletres[nova.llargaria++] = lletra;
How can I solve this??
Any ideas on how to read a line of the file using that method??
thank you in advance
***********************EDIT***********************
Here's what I've tried:
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();
}
But it keeps reporting the same exception.
The main idea of the program consists in creating a text file for each person represented in the differents line of the main file (datos_clientes.txt), the files have a different text or pattern depending on the person.
One line of the main file looks like this:
t1 #n name #d address (t1 is the pattern of the file that will be created)
After that I have to read the name of the person, save it, read the address, save it and writing it in the file that was created for them.
This is what I've done so far, just the method that creates the different files.
Hope you can help me
Thanks!!
This is the class 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;
}
}
I changed some to non-static and at least I can run it, now I'm trying to know if I'm reading the line properly. If I'm not wrong with method llegir() I can read one word and save it, right? Correct me if I'm wrong. I'm trying to read the first "word" in the line, that is "t1" for example, then I compare it to another char array and I tried to print to check if the code works, but it doesn't. What am I doing wrong??
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
}