Quick newbie Java programming question (idle, timers)

S

Starwynd

Greetings all -

I am working on programming a very very simple java application for
fun - I am still learning java, and was curious of the best way to
accomplish the following task -

I want to be able to have my Java program run, and have the user be
able to exit it at any time by typing an exit string ("exit") or
something of like. But also, I have other things running on timers in
the backgrounds - it's kind of a virtual reality game. While the game
is running, I want it to update onscreen with different messages, such
as "it is now evening" or "it is now noon", but still be able to
accept user input at any time to terminate the program. I tried adding
the system.exit(0) line to my timer such as this:

import java.util.*;
import java.io.*;


public class ShortSnowUpdate extends TimerTask
{
public ShortSnowUpdate()
{
}
public void run()
{
if (KeyboardReader.readInt() != 5)
{
System.out.println("it is snowing");
}
else
System.exit(0);



}

}


But this just sits there at the command prompt always waiting for user
input, and not actually processing the update when it occurs once a
minute (the timer event is scheduled in the manager thread to run once
a minute, and display the message "it is snowing" to the screen). It
obviously exits nicely when "5" is pressed, but it won't continue to
give me the "it is snowing" message once a minute. I just need
something that will let the program idle while responding to the
timers, but also ending if the user inputs the end input (5 in this
case) while not constantly prompting the user to enter something.Any
ideas? I know this is probably something pretty simple! :)

-Starwynd
 
T

Tor Iver Wilhelmsen

if (KeyboardReader.readInt() != 5)

Most likely, this is a blocking call. You need to put the message into
a separate thread from the one you use to read the keyboard.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top