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

Discussion in 'Java' started by Joona I Palaste, Aug 28, 2003.

  1. Peter Blatt <> 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 () ---------------------------\
    | 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, Aug 28, 2003
    #1
    1. Advertising

  2. Joona I Palaste

    Will Clark Guest

    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?

    :eek:)



    "Joona I Palaste" <> wrote in message
    news:bilc5l$oni$...
    > Peter Blatt <> 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 () ---------------------------\
    > | 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, Aug 28, 2003
    #2
    1. Advertising

  3. Will Clark <> 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?


    > :eek:)


    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 () ---------------------------\
    | 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
    Joona I Palaste, Aug 28, 2003
    #3
  4. Joona I Palaste

    Will Clark Guest

    True, true!

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

    >
    > > :eek:)

    >
    > 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 () ---------------------------\
    > | 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, Aug 28, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harvey
    Replies:
    0
    Views:
    665
    Harvey
    Jul 16, 2004
  2. Harvey
    Replies:
    1
    Views:
    817
    Daniel
    Jul 16, 2004
  3. dan
    Replies:
    1
    Views:
    595
    Joona I Palaste
    Aug 28, 2003
  4. online.no
    Replies:
    4
    Views:
    383
  5. Deepak Kannan
    Replies:
    3
    Views:
    106
    Deepak Kannan
    Dec 29, 2010
Loading...

Share This Page