• Post Reply Bookmark Topic Watch Topic
  • New Topic

Save a String to a Text File,  RSS feed

 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am Trying to Save a String to a Text File..


 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the question is...?
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i get my toString into a Text file,

 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the code you provided cannot be compiled as is. First of all, FileWriter class has no write() method, so out.write(); will prevent you from compiling the code. Also I think there is one closing brace more than it should be, but I will suppose you made a mistake when pasting the code.

There is also something that looks wrong with the logic. You are trying to write to file inside the for loop. So what the output file would contain is the amount returned only from the last element of your StaffList array increased by value of basic. However, if you wanted to save the data for each element of your StuffList array, you would have to initialize FileWriter before you enter the for loop, write the data inside the for loop, and close the output after it. Basically, it should look like this:

And what's the purpose of the code marked as Reading a file? If you need to read the file too, than you should put that in separate method. Also, naming a method holidays() for this task doesn't seem like a very good choice to me.
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I Fixed up the Loop,

but my main problem doesnt seem to want to fix itself,

Here is my String



I Used a ArrayList and got the String to Print to text file, but i cant seem to get it to work on an Array
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try this?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!