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

J

joealey2003

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.
 
R

Roedy Green

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.
 
R

Roedy Green

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
 
J

joealey2003

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...
}
//----------------------------------------------
 
R

Roedy Green

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.
 
J

joealey2003

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...
}
//----------------------------------------------
 
S

Silvio Bierman

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
 
M

Missaka Wijekoon

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();
}
}
}
 
R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top