Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java program timer countdown

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java program timer countdown

    So I'm fairly new to java and im making a text based game project using switch statements in different methods. I'm trying to put a 10 sec timer on the user input. I have the timer code working but i cannot get it to work with the user input. My code is way to long to put in this but i can show what the code looks like for the count down if that helps. the if statement doesn't work just something i was trying.


    Timer myTimer = new Timer();
    TimerTask task = new TimerTask()
    {
    public void run()
    {

    secondsPassed --;
    System.out.println("secondes passed: " + secondsPassed);

    if(str.equals("") || secondsPassed == 0)
    {
    System.exit(0);
    task.cancel();
    }


    }

    };


    public void start(){
    myTimer.scheduleAtFixedRate(task,1000,1000);
    }
    Last edited by dbob; November 21st, 2019 at 05:36 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java program timer countdown

    if statement doesn't work
    Why do you say that?

    What are the values in str and secondsPassed when the if statement is executed? Add a print statement that prints their values so you can see what the program sees when it executes.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java program timer countdown

    im sorry the if statement works correctly but it doesnt stop when the user enters an input which is what i want it to do

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java program timer countdown

    doesnt stop when the user enters an input
    The code won't call System.exit if there is a value in str.

    What is the value in str and what is the value in secondsPassed when the if statement is executed?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java program timer countdown

    the value in "str" is user input which is either "yes" or "no" the secondsPassed i have the value 10 so it counts down from 10 to 0 then it exits the code. i tried to do task.cancle(); to stop the timer but did not work.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java program timer countdown

    Did you print the value that was in str? What printed?
    What value was printed for secondsPassed?
    Please copy the value that were printed and paste it here.

    Nothing in the program will execute after System.exit is called. The program exits/
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. HELP ABOUT [COUNTDOWN] OR [TIMER] using GUI.
    By javados in forum AWT / Java Swing
    Replies: 12
    Last Post: September 29th, 2014, 09:11 PM
  2. about countdown timer and jcombobox need help please
    By javados in forum AWT / Java Swing
    Replies: 12
    Last Post: September 29th, 2014, 07:16 AM
  3. Countdown timer
    By AANZ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2013, 07:46 AM
  4. Please help me with my Java Countdown Timer
    By remagarrote in forum AWT / Java Swing
    Replies: 3
    Last Post: March 8th, 2013, 10:09 AM
  5. Timer class countdown
    By p0oint in forum Object Oriented Programming
    Replies: 5
    Last Post: August 26th, 2010, 03:31 AM