This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Printing Int Array to File using PrintWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Printing Int Array to File using PrintWriter" Watch "Printing Int Array to File using PrintWriter" New topic
Author

Printing Int Array to File using PrintWriter

Jim Hester
Ranch Hand

Joined: Sep 19, 2009
Posts: 36
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

Joined: Oct 13, 2005
Posts: 32662
    
    4
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

Joined: Feb 23, 2009
Posts: 1183
how about ...



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

JDBCSupport - An easy to use, light-weight JDBC framework -
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32662
    
    4
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

Joined: Feb 23, 2009
Posts: 1183
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

Joined: Sep 19, 2009
Posts: 36
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

Joined: Dec 10, 2009
Posts: 59
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

Joined: Feb 23, 2009
Posts: 1183
In your printArray method: use StringBuilder instead of String.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32662
    
    4
Agree with SJ. StringBuilder is usually better than StringBuffer.
Evan Caballero
Ranch Hand

Joined: Dec 10, 2009
Posts: 59
Isn't StringBuffer a sub class of StringBuilder ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32662
    
    4
No
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
stringbuilder vs stringbuffer
Evan Caballero
Ranch Hand

Joined: Dec 10, 2009
Posts: 59
nice, thanks ;)
Jim Hester
Ranch Hand

Joined: Sep 19, 2009
Posts: 36
Thanks! Wasn't aware of StringBuilder. Made the appropriate changes.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Printing Int Array to File using PrintWriter
 
Similar Threads
Passed SCPJ2 With 93%
Sun Cirtification
convert char to int in a string?
Ruby: Range conditionals
Program keeps locking up, not sure why