Error when reading and writing a line from a text file - any help!!
Hi,
My test main class allows user input to create one integer, which I've converted to a string, plus four strings representing details to be written to a file. When these details are written to the file they initially read correctly, eg:
111,hello one,hello two,hello three,hello four
The problem occurs when I try to write a second set if user entry values to this file. It is expecting four strings as words, and therefore reads and writes the above line as follows,
111,,hello,one,hello,two,hello,three,hello
I have absolutely no idea why this is happening and would really appreciate some help or advice with this problem.
The current code for my test main class is as follows,
Code:
import simplejava.*;
import videos.*;
import java.io.*;
public class AddTitle {
public static void main(String[] args) {
String film, actor1, actor2, director, title;
int codeNum;
SimpleReader keyboard = new SimpleReader();
SimpleWriter screen = new SimpleWriter();
SimpleReader inFile = new SimpleReader("title_test.txt");
TitleList titles = new TitleList(inFile);
codeNum = keyboard.readInt("Title Code for the Film");
int intValue = codeNum;
String stringValue = Integer.toString(intValue);
film = keyboard.readLine("Film Title?");
actor1 = keyboard.readLine("Lead Actor?");
actor2 = keyboard.readLine("Supporting Actor?");
director = keyboard.readLine("Director?");
titles.add(new Title(codeNum, film, actor1, actor2, director));
screen.println(titles);
title = inFile.readLine();
String[] myStrings = title.split(",");
for (int i=0; i<myStrings.length; i++)
System.out.print(myStrings[i]);
SimpleWriter outFile = new SimpleWriter("title_test.txt");
titles.saveTo(outFile);
outFile.close();
}
}
Does anybody know why this is happening?
Any help or advice really appreciated!! :confused:
Re: Error when reading and writing a line from a text file - any help!!
Hi,
When a user adds the first set of details, it prints to the screen and also saves perfectly. It is only subsequent prints and saves that don't work correctly. When the user inputs the second set of data, this new data is printed out and saved correctly, but the previous input data gets rewritten as follows,
Original input = hello world, hello world2, hello world3, hello world4
subsequent saves = hello,world,hello,world2,hello,world3,hello,world4
I think basically the problem is with the constructors in the Title class, which require four Strings, read using readWord(). Whereas, the only way I know to allow a user to input more than one word on a line via the keyboard is using readLine() in the test main class.
Does anyone know how to correct this possible conflict?
The output to the test file saves as follows after the first run and save,
Code:
20 1
111,film one,actor one,actress one,director one
and then it is changed and outputs as follows with the errors,
Code:
20 2
111,,film,one,actor,one,actress,one,director
112,film two,actor two,actress two,director two
The console put put also changes as well with errors as follows,
Code:
Borrower Count = 2 Max Borrowers = 20
Code Number = 111 Film = ,film Lead Actor = one,actor Supporting Actor = one,actress Director = one,director
Code Number = 112 Film = film two Lead Actor = actor two Supporting Actor = actress two Director = director two
one
Any help or advice gratefully appreciated!! :confused: