2

I'm not sure how to accomplish this. I want to have a timer which would keep a loop on forever but while that loop is running I want to be able to have a Scanner running and waiting for user input. Basically like a chat. Where the loop would constantly check for any new messages posted but while the loop is running the user can still submit messages.

How can I do this?

Also I dont have a GUI setup yet. This is all running in a simple command prompt (if it matters).

4 Answers 4

3

You can use Worker Thread design pattern to design this. Please refer following links to get more idea on this:

Sign up to request clarification or add additional context in comments.

Comments

1

You could use a multiple thread application, however it seems to me that would be a bit more complicated than what your looking for.

: Try just defining a timer, and adding a timertask to it, and then taking in input in a loop:

    Scanner scan = new Scanner(System.in);
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            //Do whatever you want with your messages here
        }
    }, 0, 1000);

    //Wait for user input
    while(true) {
        String bar = scan.next();
    }

4 Comments

yah but this resets the scanner every second, it displays if there are new messages but if your typing a message while that occurs i think the message scanner resets.
im not sure what you mean. The code will stay inside of the "while(true)" loop while the timer runs concurrently, the scanner shouldn't ever reset
oh, it could be because im running in a command prompt so it gives the impression that it resets, ill test it and see
yep, its because its in command prompt, while it gives the impression that it resets in in fact doesn't. Thanks for your help, Ill use this method :). Im still too new and the other methods with the worker thread and the sleep() seem a bit complicated.
0

You can have a thread running which is waiting for the input. Once the input is entered, the main program can call a method in the thread which can then decide whether to terminate the loop or keep going.

Comments

0

Well, sounds pretty early stage, but you could either do multiple Threads, or you could just have your loop bounce back and forth between new messages from the user, and checking to see if there are new things posted.

If you're reading from an InputStream (or one of its sub-classes) you can call the available method to check and see if the number of available bytes if > 0 (if there is anything to be read from either the other chat participants, or the user input).

So, a loop:

  • if (newMessages) ...post the new messages...
  • if (newUserChat) ...send new message out...

That's basically all there is to it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.