Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: "System.in.println()" not working/Replacement wanted ("in" NOT:out !!!)

Reply
Thread Tools

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

 
 
Joona I Palaste
Guest
Posts: n/a
 
      08-28-2003
Peter Blatt <(E-Mail Removed)> 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 ((E-Mail Removed)) ---------------------------\
| 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
 
Reply With Quote
 
 
 
 
Will Clark
Guest
Posts: n/a
 
      08-28-2003
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?

)



"Joona I Palaste" <(E-Mail Removed)> wrote in message
news:bilc5l$oni$(E-Mail Removed)...
> Peter Blatt <(E-Mail Removed)> 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 ((E-Mail Removed)) ---------------------------\
> | 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



 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      08-28-2003
Will Clark <(E-Mail Removed)> 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?


> )


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 ((E-Mail Removed)) ---------------------------\
| 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
 
Reply With Quote
 
Will Clark
Guest
Posts: n/a
 
      08-28-2003
True, true!

"Joona I Palaste" <(E-Mail Removed)> wrote in message
news:bilmld$qr$(E-Mail Removed)...
> Will Clark <(E-Mail Removed)> 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?

>
> > )

>
> 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 ((E-Mail Removed)) ---------------------------\
> | 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
connection not wanted? Frank Wireless Networking 10 07-14-2005 10:45 PM
WANTED: AT desktop cases, wanted for sculpture. Brett NZ Computing 0 09-09-2004 07:14 AM
HELP WANTED HELP WANTED HELP WANTED Harvey ASP .Net 1 07-16-2004 01:12 PM
HELP WANTED HELP WANTED HELP WANTED Harvey ASP .Net 0 07-16-2004 10:00 AM
Re: "System.in.println()" not working/Replacement wanted ("in" NOT:out !!!) dan Java 1 08-28-2003 07:04 PM



Advertisments