• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem saving data to text file correctly

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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!
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think we need to see the code for SimpleReader...
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic