• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

PrintWriter(OutputStream) cutoffs

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic