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:
(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)for (PrintWriter writer : writers) { writer.println("NAMES" + buffer.toString()); }
-Sorry for the wall of text.