Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Re: "System.in.println()" not working/Replacement wanted ("in" NOT:out !!!) (http://www.velocityreviews.com/forums/t126136-re-system-in-println-not-working-replacement-wanted-in-not-out.html)

Joona I Palaste 08-28-2003 04:55 PM

Re: "System.in.println()" not working/Replacement wanted ("in" NOT:out !!!)
 
Peter Blatt <peterb@gmx.net> scribbled the following
on comp.lang.java.programmer:
> I want to read one single text line from System.in (with buf.radLine())
> which was previously NOT typed by the user but written from another thread.


> For that a command like


> System.in.println();


> would be perfect on the pipewriting side (Keep an eye on "in" NOT "out" !!).
> However this command does not exist.


> Unfortunately I dont't want permanently write to System.in. resp.
> occasionally I want to read from user console as well. Because of this
> PipedStreams were not suitable because I cannot switch from user console
> to PipedStreams.


> Does anyone know a working replacement for the command above ?


Write a Runnable class, which has an in-built line buffer. Implement
three methods:
- run(): Keeps reading lines from System.in, and writing them to your
buffer,
- println(): Writes lines directly to your buffer, and
- readln(): Reads a line from your buffer.
Then you can just keep calling readln() (which can be a blocking call),
and you can get lines as lines themselves, making no difference whether
they came from the user console or an explicit call to println().

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"It's time, it's time, it's time to dump the slime!"
- Dr. Dante

Will Clark 08-28-2003 07:24 PM

Re: "System.in.println()" not working/Replacement wanted ("in" NOT:out !!!)
 
I don't think you'd even necessarily need a separate thread to run. If you
had (hypothetically, cos I don't have time to try it out!) a buffer that a
call to println() could fill up as necessary, then when the readln() is
called it could first check to see if there is anything in the buffer, and
return that, or call a readln() on the System.in stream (see that other post
in this thread on how to use BufferedReader) and return any value
forth-coming from there...

(pause for breath)

Did that make sense?

:o)



"Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message
news:bilc5l$oni$1@oravannahka.helsinki.fi...
> Peter Blatt <peterb@gmx.net> scribbled the following
> on comp.lang.java.programmer:
> > I want to read one single text line from System.in (with buf.radLine())
> > which was previously NOT typed by the user but written from another

thread.
>
> > For that a command like

>
> > System.in.println();

>
> > would be perfect on the pipewriting side (Keep an eye on "in" NOT "out"

!!).
> > However this command does not exist.

>
> > Unfortunately I dont't want permanently write to System.in. resp.
> > occasionally I want to read from user console as well. Because of this
> > PipedStreams were not suitable because I cannot switch from user console
> > to PipedStreams.

>
> > Does anyone know a working replacement for the command above ?

>
> Write a Runnable class, which has an in-built line buffer. Implement
> three methods:
> - run(): Keeps reading lines from System.in, and writing them to your
> buffer,
> - println(): Writes lines directly to your buffer, and
> - readln(): Reads a line from your buffer.
> Then you can just keep calling readln() (which can be a blocking call),
> and you can get lines as lines themselves, making no difference whether
> they came from the user console or an explicit call to println().
>
> --
> /-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
> | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
> | http://www.helsinki.fi/~palaste W++ B OP+ |
> \----------------------------------------- Finland rules! ------------/
> "It's time, it's time, it's time to dump the slime!"
> - Dr. Dante




Joona I Palaste 08-28-2003 07:54 PM

Re: "System.in.println()" not working/Replacement wanted ("in" NOT:out !!!)
 
Will Clark <willz@btopenworld.com> scribbled the following
on comp.lang.java.programmer:
> I don't think you'd even necessarily need a separate thread to run. If you
> had (hypothetically, cos I don't have time to try it out!) a buffer that a
> call to println() could fill up as necessary, then when the readln() is
> called it could first check to see if there is anything in the buffer, and
> return that, or call a readln() on the System.in stream (see that other post
> in this thread on how to use BufferedReader) and return any value
> forth-coming from there...


> (pause for breath)


> Did that make sense?


> :o)


What if println() is called after the user has typed a line? Your
version would return the println()ed line first, and the one typed by
the user later, in contradiction to their real-time order. Mine would
have more chance of returning them in the right order.

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"The question of copying music from the Internet is like a two-barreled sword."
- Finnish rap artist Ezkimo

Will Clark 08-28-2003 08:45 PM

Re: "System.in.println()" not working/Replacement wanted ("in" NOT:out !!!)
 
True, true!

"Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message
news:bilmld$qr$1@oravannahka.helsinki.fi...
> Will Clark <willz@btopenworld.com> scribbled the following
> on comp.lang.java.programmer:
> > I don't think you'd even necessarily need a separate thread to run. If

you
> > had (hypothetically, cos I don't have time to try it out!) a buffer that

a
> > call to println() could fill up as necessary, then when the readln() is
> > called it could first check to see if there is anything in the buffer,

and
> > return that, or call a readln() on the System.in stream (see that other

post
> > in this thread on how to use BufferedReader) and return any value
> > forth-coming from there...

>
> > (pause for breath)

>
> > Did that make sense?

>
> > :o)

>
> What if println() is called after the user has typed a line? Your
> version would return the println()ed line first, and the one typed by
> the user later, in contradiction to their real-time order. Mine would
> have more chance of returning them in the right order.
>
> --
> /-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
> | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
> | http://www.helsinki.fi/~palaste W++ B OP+ |
> \----------------------------------------- Finland rules! ------------/
> "The question of copying music from the Internet is like a two-barreled

sword."
> - Finnish rap artist Ezkimo





All times are GMT. The time now is 12:48 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.