Results 1 to 14 of 14
  1. #1
    eel
    eel is offline Member
    Join Date
    Sep 2010
    Posts
    36
    Rep Power
    0

    Default Help with separator

    Hi, I would like to use separator but the results come out were not what I expected.

    I use
    Tos = To.split(",");
    for (int i = 0; i < Tos.length; i++)
    {
    System.out.println(Tos[i]);
    }
    I was using printwriter as well. I hope that in the file the result should look like this:
    To:ll
    To:ooo
    To:pp

    Instead of
    To:ll,ooo,pp

    Is anyone know what's the problem?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Please edit your post and use [code] ... [/code] tags instead of those smilies so we can actually read what you wrote; also show us your original String 'To'.

    kind regards,

    Jos

  3. #3
    eel
    eel is offline Member
    Join Date
    Sep 2010
    Posts
    36
    Rep Power
    0

    Default

    System.out.println("To who?");
    To = in.nextLine();
    Tos = To.split(",");
    for (int i = 0; i < Tos.length; i++)
    {
    System.out.println(Tos[i]);
    }

    I hope that in the file the result should look like this:
    To:ll
    To:lpp
    To:lii
    Instead of
    To:ll,lpp,lii

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by eel View Post
    System.out.println("To who?");
    To = in.nextLine();
    Tos = To.split(",");
    for (int i = 0; i < Tos.length; i++)
    {
    System.out.println(Tos[i]);
    }

    I hope that in the file the result should look like this:
    To:ll
    To:lpp
    To:lii
    Instead of
    To:ll,lpp,lii
    What is the exact code you're using to print to a file? because the code you're showing us prints each part of the String on a separate line and no "To:" prompt. Your code doesn't even print to a file; please give us all the details we need to help you.

    kind regards,

    Jos

  5. #5
    eel
    eel is offline Member
    Join Date
    Sep 2010
    Posts
    36
    Rep Power
    0

    Default

    The code I use to print file is as below:
    pwInput.println("To: " + To);

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by eel View Post
    The code I use to print file is as below:
    pwInput.println("To: " + To);
    Are you surprised about its output? Where is the code you posted in your original post? Now you are simply printing the value of the original String 'To' prepended by the String "To: ".

    kind regards,

    Jos

  7. #7
    eel
    eel is offline Member
    Join Date
    Sep 2010
    Posts
    36
    Rep Power
    0

    Default

    pwInput.println("To: " + To);
    String delimiter = ",";
    if (To.equals(",")) {
    rr = To.split(delimiter);
    for (int i = 0; i < rr.length; i++)
    {
    pwInput.println("To: " + rr[i]);
    }}

    This isn't work as well.

    I actually want the output to be:
    To:ll
    To:lpp
    To:lii

    Instead of
    To:ll,lpp,lii

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by eel View Post
    pwInput.println("To: " + To);
    String delimiter = ",";
    if (To.equals(",")) {
    rr = To.split(delimiter);
    for (int i = 0; i < rr.length; i++)
    {
    pwInput.println("To: " + rr[i]);
    }}
    Read it out loud: the if-condition reads: "if String 'To' is equal to a single comma". If it isn't the entire body of the if statement is skipped.

    kind regards,

    Jos

  9. #9
    eel
    eel is offline Member
    Join Date
    Sep 2010
    Posts
    36
    Rep Power
    0

    Default

    So how do I make the printwriter to be the way I want if the string contains comma?

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by eel View Post
    So how do I make the printwriter to be the way I want if the string contains comma?
    Think a bit: if your String is equal to "a,b,c" you want to output to be:

    Java Code:
    To: a
    To: b
    To: c
    If the String equals "a", do you want the output to be:

    Java Code:
    To: a
    ? If so, have a look at the String.split( ... ) method again, if not, please explain what you want.

    kind regards,

    Jos

  11. #11
    eel
    eel is offline Member
    Join Date
    Sep 2010
    Posts
    36
    Rep Power
    0

    Default

    My problem is the code
    To: a
    To: b
    To: c
    showed in the command window instead of showing in the printwriter.
    I don't know why.

  12. #12
    eel
    eel is offline Member
    Join Date
    Sep 2010
    Posts
    36
    Rep Power
    0

    Default

    It shows in command window as:
    To:a
    To:b
    To:c
    but it shows in the file as:
    To:a,b,c

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by eel View Post
    It shows in command window as:
    To:a
    To:b
    To:c
    but it shows in the file as:
    To:a,b,c
    You are using the println( ... ) method when you print to the console but you are using print( ... ) when you're writing to a file; you should print an end-of-line character to the file as well for every line. And please show us the actual code you are using because as it is now the entire thread becomes confusing at best.

    kind regards,

    Jos

  14. #14
    eel
    eel is offline Member
    Join Date
    Sep 2010
    Posts
    36
    Rep Power
    0

Similar Threads

  1. Remove separator between windows
    By Annie George in forum NetBeans
    Replies: 0
    Last Post: 09-09-2010, 09:04 AM
  2. Replies: 0
    Last Post: 04-04-2008, 02:48 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
  •