• Post Reply Bookmark Topic Watch Topic
  • New Topic

PrintWriter(OutputStream) cutoffs

 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys -

Pretty basic question. I'm using a PrintWriter attached to an OutputStream, with a readLine loop that looks like this:


The BufferedReader and PrintWriter are both attached to to Socket streams. Output loop server-side looks like this:



The problem is that the strings are always cut off at the last \n, with the remainder to be included in the next readLine, which is unabridged save for what lays beyond its own final "\n".

Thoughts?

EDIT: Here's an example of a string that is severed.
"Which profession is your character?\n" +
"Choose from:\n1) Monk\n2) Psionist\n3) Berserker\n4) Mage";

In this case, "\n4)Mage" is transplanted.

EDIT2: So I decided to try and rewrite the whole thing with @n instead of \n, and then build my own code for recognizing @n as \n. Now the entire line, before and after @n, gets transplanted to the next Writer.println(). How does that make sense at all? o.O
[ December 28, 2008: Message edited by: Alex Birmingham ]
 
Peter Lawrey
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
readLine() strips the newlines at the end so to put them back use println().
 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you elaborate a bit, please? Use println where?

If you mean here:



That doesn't do anything different.
 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem solved by encapsulating PrintWriter into a thread with a while loop and it's own stack of strings to print (as opposed to a sequential write method which was part of the server's ordinary routine.) Not entirely sure why this solution worked, frankly, because the original problem seemed to have more to do with the operating system's zaney idea about where to end a line.

Nevertheless, it worked.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!