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 4 of 4

Thread: Need Help With Code for Kitchen Timer

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Need Help With Code for Kitchen Timer

    I have a problem. I tried debugging and I saw nothing pop up. Something is wrong but I cant seem to find it since its not a syntax error. I rechecked everything and I still couldnt find the problem.

    Here is what the code is supposed to do.
    1) Take in the user's integer value.
    2) Print the values as they count with 1 second interval.
    3) When it reaches one a message pops up and says "Time's up!!".

    However when i run it, it asks for input and pops up the message immediately. There is no printing of the countdown, nor is there any one second interval. I have two classes, the main class and a class with all the functions. They names will help you tell what they do from the start.

    Here is the class with the functions.

    package kitchentimer;
    import java.util.Scanner;
    import javax.swing.JOptionPane;



    public class Timer {
    boolean decide=false;
    Scanner input = new Scanner(System.in);

    public void getTime(int t){
    System.out.println("Set the timer value in seconds");
    t=input.nextInt();


    }

    public void runTimer (int tmrValue){
    System.out.println("The timer has started");
    tmrValue++;

    for (int x=1; x<tmrValue{

    System.out.println(tmrValue);
    Thread.sleep(1000);
    tmrValue-=1;

    }

    if(tmrValue==1){
    decide=true;
    }

    }
    public void timerEnd (){
    if (decide==true){
    JOptionPane.showMessageDialog(null,"Time is up!!");
    }

    }

    }




    Here is the main class


    package kitchentimer;


    public class KitchenTimer {

    public static void main(String[] args) {
    int TimeValue=0;
    Timer TimerObject = new Timer();
    TimerObject.getTime(TimeValue);
    TimerObject.runTimer(TimeValue);
    TimerObject.timerEnd();

    }
    }

    Please help me find the problem so I can avoid it in the future.Timer.zipTimer.zip


  2. #2
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help With Code for Kitchen Timer

    Alright, I already know that I have to throw the Thread.sleep function in a try statement, I've done that already, its not the problem. Please help me

  3. #3
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Need Help With Code for Kitchen Timer

            public void getTime(int t) {
    		System.out.println("Set the timer value in seconds");
    		t = input.nextInt();
     
    	}
    That method does exactly nothing. You never use t.

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help With Code for Kitchen Timer

    Thanks for the reply. I probably wasn't thinking straight this morning. Ill check it out.

    --- Update ---

    Quote Originally Posted by PhHein View Post
            public void getTime(int t) {
    		System.out.println("Set the timer value in seconds");
    		t = input.nextInt();
     
    	}
    That method does exactly nothing. You never use t.

    t was used to store the input value. I don't get what you're saying, please explain.

    --- Update ---

    I have removed all the variables and used a private variable. The problem was that my variable from the input method was not returned to the runTimer method so the local variable was equal to 0 and the code in that class was skipped altogether. When I used the single class variable in all the methods the value input was returned to the other methods. Thank you.

Similar Threads

  1. Timer Countdown - Code Syntax
    By baddack in forum What's Wrong With My Code?
    Replies: 22
    Last Post: September 26th, 2012, 03:56 PM
  2. Timer code does not work!
    By mariapatrawala in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2010, 10:03 AM
  3. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM
  4. How to Use Timer in Java
    By neo_2010 in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: September 1st, 2009, 12:10 PM

Tags for this Thread