While(UserInput != 'S') without stopping the execution.

Discussion in 'Java' started by joealey2003@yahoo.com, Oct 1, 2005.

  1. Guest

    Hi all.

    Every time you call a inputstream.read(), it stops the execution and
    wait for the user input. How can i keep it running and check if the
    user has typed something?

    I tried :

    if (System.in.available() > 0) but i need to read to change the
    available value....

    How can i do that?
    I need something like a main loop control:

    while(UserInputChar != 'S'){
    //RUN
    Thread.currentThread.sleep(5000);
    }
    //----------------------------------------------

    Thanks.
    , Oct 1, 2005
    #1
    1. Advertising

  2. Roedy Green Guest

    On 30 Sep 2005 23:28:33 -0700, wrote or quoted :

    > Every time you call a inputstream.read(), it stops the execution and
    >wait for the user input. How can i keep it running and check if the
    >user has typed something?


    That is exactly what it is doing already.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 1, 2005
    #2
    1. Advertising

  3. Roedy Green Guest

    On 30 Sep 2005 23:28:33 -0700, wrote or quoted :

    >
    > while(UserInputChar != 'S'){
    > //RUN
    > Thread.currentThread.sleep(5000);


    Java does not work that way. There is no such thing as single char
    input. And readln blocks (waits) all my itself for some input. It
    does not return to you until it has a line.

    You are coding this as if it were Forth.

    for samples of i/o see http://mindprod.com/applets/fileio.html

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 1, 2005
    #3
  4. Guest

    Re: While(UserInput != 'S') without stopping the execution.

    but it will stop the execution....
    i need to continue if the user has not typed nothing...

    while(UserInputChar != 'S'){
    Thread.currentThread.sleep(5000);
    UserInputChar = System.read //<<<-- Don't stop if there
    is nothing typed already...
    }
    //----------------------------------------------
    , Oct 1, 2005
    #4
  5. Roedy Green Guest

    Re: While(UserInput != 'S') without stopping the execution.

    On 30 Sep 2005 23:47:52 -0700, wrote or quoted :

    >but it will stop the execution....
    >i need to continue if the user has not typed nothing...


    there are no console i/o commands to do that. This is not C or
    Assembler. Some platforms have no single char i/o, so Java being
    multiplatform does not support it. To get those sorts of effects you
    need to write a GUI where people type code into boxes while the
    program continues in the background and gets interrupted with events
    to let it know of changes to the values.

    It seems like the most incredible song and dance to do what you want,
    but once you get the hang of it you can know out the reams of
    necessary bubblegum.

    Search the web for some source code using TextField or JTextField. All
    you need is a minimal Frame and TextField. I have a number of applets
    with source posted at
    http://mindprod.com/jgloss/applets/amanuenses.html

    many have a TextField, TextArea, JTextField or JTextArea. The problem
    is they might do too many other things that would be confusing.

    Perhaps Sun's tutorial might have some suitably stripped down code to
    show you how to do a TextField.

    Any intro to AWT or Swing text will show you how to do this.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 1, 2005
    #5
  6. Guest

    Re: While(UserInput != 'S') without stopping the execution.

    but it will stop the execution....
    i need to continue if the user has not typed nothing...

    while(UserInputChar != 'S'){
    Thread.currentThread.sleep(5000);
    UserInputChar = System.read //<<<-- Don't stop if there
    is nothing typed already...
    }
    //----------------------------------------------
    , Oct 1, 2005
    #6
  7. Guest

    , Oct 1, 2005
    #7
  8. Guest

    , Oct 1, 2005
    #8
  9. Roedy Green Guest

    Roedy Green, Oct 1, 2005
    #9
  10. Re: While(UserInput != 'S') without stopping the execution.

    writes:

    > but it will stop the execution....


    .... of that particular thread. There's a hint right there.
    Tor Iver Wilhelmsen, Oct 1, 2005
    #10
  11. Use a separate thread for the ongoing stuff and do synch IO (and
    manipulation of the ongoing stuff thread) in the main thread.

    Java has only very limited console support.

    Silvio Bierman

    <> wrote in message
    news:...
    >
    > Hi all.
    >
    > Every time you call a inputstream.read(), it stops the execution and
    > wait for the user input. How can i keep it running and check if the
    > user has typed something?
    >
    > I tried :
    >
    > if (System.in.available() > 0) but i need to read to change the
    > available value....
    >
    > How can i do that?
    > I need something like a main loop control:
    >
    > while(UserInputChar != 'S'){
    > //RUN
    > Thread.currentThread.sleep(5000);
    > }
    > //----------------------------------------------
    >
    > Thanks.
    >
    Silvio Bierman, Oct 1, 2005
    #11
  12. wrote:
    > Hi all.
    >
    > Every time you call a inputstream.read(), it stops the execution and
    > wait for the user input. How can i keep it running and check if the
    > user has typed something?
    >


    Hi,

    Your assumption seems OK. The following test program I compiled spins
    CPU cycles waiting for user input, i.e. it does not block. Which I
    believe is the result you want, right? Perhaps what is not under your
    control is that the shell that launched the java class is buffering user
    input. Depending on your platform, the shell may be changed to
    unbuffered (but not through java).


    import java.io.*;

    public class TestSysIn {

    public static void main(String[] args) {
    int numBytes;
    byte[] buf = new byte[1];

    try {
    while(true) {
    numBytes = System.in.available();
    if (numBytes > 0) {
    System.in.read(buf, 0, 1);
    System.out.println("Read: " + new String(buf));
    }
    // Do something else here...
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    Missaka Wijekoon, Oct 1, 2005
    #12
  13. Roedy Green Guest

    On Sat, 01 Oct 2005 13:02:36 -0700, Missaka Wijekoon
    <> wrote or quoted :

    > try {
    > while(true) {
    > numBytes = System.in.available();
    > if (numBytes > 0) {
    > System.in.read(buf, 0, 1);
    > System.out.println("Read: " + new String(buf));
    > }
    > // Do something else here...
    > }


    The difficulty comes in that "something else" has to break itself into
    tidy little parcels of work, once per loop.

    A more general solution, but rather un-Javaesque would be have two
    threads, one for your background one and one that waits for Java
    input. It would block. It would not use .available or sleep. When it
    got something it could call a method to deal with the input, the go
    back to waiting for input. The effect of calling that method
    presumably would eventually have some effect on the background thread
    -- .e.g. persuade it to shutdown.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 2, 2005
    #13
    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. Replies:
    2
    Views:
    358
  2. James Colannino

    Stopping Execution

    James Colannino, Nov 10, 2005, in forum: Python
    Replies:
    4
    Views:
    382
  3. BigHand
    Replies:
    6
    Views:
    848
    David Shapiro
    Jun 10, 2009
  4. a
    Replies:
    2
    Views:
    244
  5. Phil
    Replies:
    2
    Views:
    246
    Martin Honnen
    Aug 14, 2003
Loading...

Share This Page