Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: New line and StringBuilder

  1. #1
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default New line and StringBuilder

    I've made a client-server chat program - many clients to one server.
    To end it off, I made a JTextArea to store information about all clients currently connected to the server.

    My method is not the best, but in theory should work.
    I iterate through an ArrayList, dumping all objects (which are Strings) into the StringBuilder and then broadcast the StringBuilder object.toString() to all clients.

    By itself, like that, it does work fine. I get a single line of all the clients.

    My problem is that I want a new line after each user name, but where ever I try adding either "\n" or System.getProperty("line.separator") it will only ever broadcast the first entry in the ArrayList each time.

    I got this issue each time I tried other approaches such as broadcasting straight out of the array, or basic concatenation.

    My question is: Do PrintWriter's read new lines as end or whatever?

    Heres a brief example of when my program goes wrong:

                            while (arrayIterator.hasNext()) {
                                buffer.append(arrayIterator.next().toString());
                                //buffer.append(NEW_LINE);

    when I uncomment NEW_LINE and add it into the buffer, it will only print the first arraylist element when using the printwriter to send.(new line = System.getProperty("line.separator"))

    Broadcast with:
                                for (PrintWriter writer : writers) {
                                    writer.println("NAMES" + buffer.toString());    
                            }
    (I tried sending over the ArrayList from server to clients with a static getter method so i could dump straight from the client side into the JTextArea but always kept returning empty)

    -Sorry for the wall of text.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: New line and StringBuilder

    This is probably a long shot, but where is the end to your while loop?

    Are you adding the NEW_LINE every time?

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: New line and StringBuilder

                            while (arrayIterator.hasNext()) {
                                buffer.append(arrayIterator.next().toString());
                                //buffer.append(NEW_LINE);
                              }

    Sorry, missed out the last bracket, it ends there.
    When i leave it commented, it will add the all the clients onto my JTextArea on a single line,
    If I uncomment it, it prints the first elements and then nothing.

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: New line and StringBuilder

    -Edit, by just printing out the StringBuilder with "\n" added, results do print out on new lines. So its bound to be a PrintWriter issue perhaps?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: New line and StringBuilder

    How are the clients reading the input? Are they expecting and reading a single line, or multiple lines from the server? The println should not evaluate the new line characters in the string, so I am thinking of the scenario where all the data is being sent, but the client reads the data in a manner which doesn't capture anything after the first new line.

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: New line and StringBuilder

    A thread(with a runnable target) is constantly running checking for server responses:

    where line = in.readLine();

                                    else if (line.startsWith("NAMES")) {
                                        clientLister.setText(line.substring(5));
                                    }

    Server sends message starting with NAMES, so it receives the input - "NAMES".

    As I'm sending the whole buffer in one, I only need to add this to the JTextArea once.
    But If I try and place \n or line.separator into the buffer it stops printing everything that should come after the first "\n".

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: New line and StringBuilder

    It seems your problem is then client side. The server will send content like this
    NAMES name1
    name2
    name3
    name4

    The client then reads these line by line, the first line the only one containing the "NAMES" tag, and thus the only one parsed and interpreted correctly.

  8. #8
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: New line and StringBuilder

    So do StringBuilders send it segments?

    I took on what you said and tried all the following:
    A)added "NAMES" onto each arraylist element
    B)add Names before each String in the StringBuffer
    and i either get the same result as I did to start with
    or i got name1NAMES name2

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: New line and StringBuilder

    Quote Originally Posted by newbie View Post
    So do StringBuilders send it segments?
    No. You are calling read line, which reads the stream until the next line delim is found. From what I understand based upon the code and explanations posted, you are reading in segments - each segment being a line. If you print a bunch of lines server side, the client must read a bunch of lines to get all the information. Rather than fuss with all the new lines, try the following experiment: instead of appending a new line after each element, append some other delimiter (say for example a semi-colon or tab space). Then when the client reads the line that begins with NAMES, split the line based upon this delimiter. A bit crude of a hack, but could be a short term solution (or at least something to pick apart the code)

  10. The Following User Says Thank You to copeg For This Useful Post:

    newbie (January 1st, 2011)

  11. #10
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: New line and StringBuilder

    Your advice worked great!
    I really do appreciate the time and help you gave me, this had been bothering me for ages.
    Thanks a million!
    Last edited by newbie; January 1st, 2011 at 09:12 PM.

Similar Threads

  1. Get a certain line in a JTextField
    By FlamingDrake in forum Java Theory & Questions
    Replies: 2
    Last Post: May 14th, 2010, 03:21 PM
  2. Replies: 2
    Last Post: February 25th, 2010, 03:17 PM
  3. How to reverse a String using java.lang.StringBuilder
    By JavaPF in forum Java SE API Tutorials
    Replies: 0
    Last Post: July 22nd, 2009, 09:42 AM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM