Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing Int Array to File using PrintWriter

 
Jim Hester
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm hoping this is a simple fix and I'm just overlooking something. In the PrintArray method, I really want to send that output to the file, but pw.print won't work. Does the method not inherit or know that I've already initialized the PrintWriter?? What's a way to fix this other than hardcoding it in?

 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you use a Formatter if you are writing plain text to a file? It's easier to use than a PrintWriter?
Anyway, wouldn't you use a BufferedWriter wrapped round a FileWriter?
Why are you starting the names of your methods with Capital Letters?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about ...



One thing... the Java Code Convention defines that class names and constructors start with a capital letter, methods don't. ;-)
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks pretty disastrous, I am afraid.

Just from memory, so you will have to check the details in the API or the Java™ TutorialsI am pretty sure I have got most of the method names wrong, but you can check that in the API. You will notice that you can add "true" as a parameter to the FileWriter constructor; it tells you in the API what that means. A Formatter is still probably easier to use. You will also have to put something in the catch. And also work out why I have put two "try"s inside each other with a "finally".
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian Janisch wrote:how about ...



One thing... the Java Code Convention defines that class names and constructors start with a capital letter, methods don't. ;-)


I forgot one thing..

either the print array method returns a string with whatever needs to be written to the file or you set the system out to your PrintWriter and leave the method the way it is ...

that would be

 
Jim Hester
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks much. I just modified the method so it returned a string, and went from there. I also fixed the naming conventions. Final code below. Thanks!!

 
Evan Caballero
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should re-write your printArray method like this, because String is immutable. A new String instance is created each time you call a method on it.
The use of StringBuffer is preferred ;)
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your printArray method: use StringBuilder instead of String.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with SJ. StringBuilder is usually better than StringBuffer.
 
Evan Caballero
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't StringBuffer a sub class of StringBuilder ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stringbuilder vs stringbuffer
 
Evan Caballero
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nice, thanks ;)
 
Jim Hester
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Wasn't aware of StringBuilder. Made the appropriate changes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic