#Trying to create an Timer that ends a method after its done

1 messages ยท Page 1 of 1 (latest)

quaint juniperBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
I'm currently making a minigame in my code. This game supposed to have a 20 second timer in it. The timer is supposed to to stop if the user made an input. If the timer runs out the should be a little text which says "Timer endet" and the method should be

completes the game before the 20 seconds are up. Here is an example of how you can achieve this using a TimerTask in Java:

import java.util.Timer;
import java.util.TimerTask;

public class Game {
    private boolean completed = false;

    public void startGame() {
        Timer timer = new Timer();
        
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if(!completed) {
                    System.out.println("Time's up! Game over.");
                    // Add code here to end the game or perform any other actions
                }
            }
        }, 20000); // 20 seconds

        // Your game logic here
        
        // If user completes the game before 20 seconds, set completed to true
        completed = true;
        
        // Cancel the timer so it doesn't execute the task after completion
        timer.cancel();
    }

    public static void main(String[] args) {
        Game game = new Game();
        game.startGame();
    }
}

In this example, we create a Timer object and schedule a TimerTask to run after 20 seconds. If the user completes the game before the time is up, we set the completed flag to true and cancel the timer to stop it from executing. If the user does not complete the game within 20 seconds, it will display a message saying "Time's up! Game over."

#

I uploaded your attachments as Gist.

hoary canyon
#

Your code has a few problems
Why do you use an atomic boolean at the first place?
In fact, why do you have a field at the first place, this doesn't make any sense

if (scanner.hasNextInt())
That's not how a scanner works, you have to read
executor.shutdownNow();
If you shutdown now, shouldn't it shutdown now and not do the task you asked it to do?

And at the end, I have no idea what your code does, where is the code which is supposed to execute within a time limit?

quaint juniperBOT
#

@broken elbow

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure ๐Ÿ‘