Results 1 to 4 of 4

Thread: output

  1. #1
    Camden is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default 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 08:04 PM.

  2. #2
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Java 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();

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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();

  4. #4
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Sorry i misread.
    extra note:

    from hardwired:
    Java 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.

Similar Threads

  1. Output Redirection
    By Sixtease in forum New To Java
    Replies: 8
    Last Post: 12-29-2008, 10:18 AM
  2. Writing an XML output
    By JThangiah in forum XML
    Replies: 2
    Last Post: 03-27-2008, 04:15 PM
  3. Output to excel
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 01:19 AM
  4. How to redirect the output
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:30 PM
  5. get the output from whoami
    By gary in forum Advanced Java
    Replies: 2
    Last Post: 06-12-2007, 01:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •