wood burning stoves 2.0*
The moose likes Java in General and the fly likes Problem saving data to text file correctly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem saving data to text file correctly" Watch "Problem saving data to text file correctly" New topic

Problem saving data to text file correctly

celine scarlett
Ranch Hand

Joined: Nov 06, 2005
Posts: 93

When a user inputs data via a keyboard in the test main class, and subsequently saves this data to a text file, an extra space is added before the string data. The text file is saved in the following format,

Each subsequent addition and saving of data to the file, adds a further space before the string in the text file. Therefore, each time some data is added and saved to the text file, another space is also saved. I really don't see why this happening.

The code for the constructors in the video class is as follows

The method for saving the data to the text file is as follows,

The strange thing, is that when the data is saved to the text file, the addition of an extra space before the string does not affect the first and last line of the text file.

This is all rather puzzling at the moment. Any ideas or advice as to why this might be happening?
celine scarlett
Ranch Hand

Joined: Nov 06, 2005
Posts: 93

I don't know whether this helps or not, but the current code for my main class is as follows,

Can anybody see why this problem might be occurring?

Any help or advice appreciated!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Just a guess - see if the readline() that reads the last token on each line preserves the space in front of the word. You might be getting the extra space there.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
celine scarlett
Ranch Hand

Joined: Nov 06, 2005
Posts: 93

Thanks for the reply. Each time the data is saved to the text file, only the first and last lines are saved in the correct format. How would I check whether the readLine()is saving a space before the string on each line?

If it is saving the space before the string when saving the data, any ideas how I can remove or prevent this from happening?

It is all rather puzzling at the moment!
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
I think we need to see the code for SimpleReader...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ryan Zezeski

Joined: Mar 13, 2005
Posts: 29
Yea, the problem doesn't seem to be in the code you provided, seeing SimpleReader would help. Also does the number of spaces incrememnt for each line, or is it always one space at the beginning of the line?

Also, did you try using trim() ?
[ December 04, 2005: Message edited by: Ryan Zezeski ]

Java has 99 problems but a pointer ain't one
I agree. Here's the link: http://aspose.com/file-tools
subject: Problem saving data to text file correctly
Similar Threads
Video store program - persisting problems. Any advice !!
Calling an object from another class and printing out result - any help?
Boolean vlaue for a video store program - please help!!
Problem when reading a CSVs file as separate words - any help!!
Creating a pointer/reference to another object - any help!!