Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-01-2007, 09:00 PM
Member
 
Join Date: Nov 2007
Posts: 11
Camden is on a distinguished road
output
I want to output (.txt document) a String Array (using PrintWriter) (every line=1 String). How can i not include the Strings that are empty?

Last edited by Camden : 12-01-2007 at 09:04 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-01-2007, 09:05 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Code:
PrintWriter wrt = new PrintWriter(new File("test.txt")); String[] tst = new String[]{"foo","foo","bar"}; // clear the file wrt.write(""); for (int i = 0; i < tst.length; i ++) { wrt.append(tst[i]+"\n"); } wrt.close();
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-01-2007, 11:28 PM
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
Code:
PrintWriter pw = new PrintWriter("testOutput.txt"); String[] strs = { "hello", "world", "", "circus" }; for(int j = 0; j < strs.length; j++) { if(!strs[j].equals("")) pw.println(strs[j]); } pw.close();
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-01-2007, 11:34 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Sorry i misread.
extra note:

from hardwired:
Code:
if(!strs[j].trim().equals("")) pw.println(strs[j]);
if you add the trim(), you are making sure that if the string array has a space element (so " ") it will not be put in the file.

Also, String.length() yields better performance.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing an XML output JThangiah XML 2 03-27-2008 05:15 PM
Output Redirection Sixtease New To Java 2 03-22-2008 10:59 AM
Output to excel abhiN New To Java 2 03-07-2008 02:19 AM
How to redirect the output JavaBean Java Tips 0 10-04-2007 10:30 PM
get the output from whoami gary Advanced Java 2 06-12-2007 02:05 PM


All times are GMT +3. The time now is 09:44 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org