If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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.

Do the added titles and information print out to the screen properly with your screen.print() method call? If so, then it seems there is a problem in the structure of your file which is being read back into the part of your code before your System.out.println() call.

What does the stored copy of your file look like? Try writing to disk before you do your System.out.println() segment, close the stream, then re-open to do the last bit ...

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,

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,

A difficulty in reading your code is that you have thrown all your code into one body, rather than having the modules separated (separation of concerns).

Your postulation sounds to be accurate, to me. When you "readWord()", you are reading only to whitespace. When you "readLine()", you read to a linefeed or newline character. So if you are reading from the data written to disk using the objects created using the constructor methods you mention, you have not grabbed all of the content intended for each of the data members - you've only grabbed one word ...

Sounds like you need to go back to your constructor.

Remember you need to flush or perform an extra "readLine" (whose content gets dumped) after grabbing an int or anything shorter than a full line - you have not grabbed the newline character, and that is why you are getting the empty string followed by a comma.

It it my recollection, reinforced by a look at the API documentation on the split() method, that you must use a "regex pattern" to do the search - so try using "\," as your "string" for the split delimiter.

No luck. It produces an error 'illegal escape character' if you use "\,"

This is really starting to annoy me.

All I'm trying to do is allow user input within the main class to add one integer and four strings(each of which can be any number of words on a line), save them as a comma seperated list and then be able to repeat this as many times as possible, including reading in existing records and add new ones.

You are calling "readInt()" to get the integer, so you are getting the content (because it is reading up to the white space) but leaving behind the new line marker. You need to get rid of the new line marker from your input stream so that when you do your first readLine() you are at the start of a new line.