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

Thread: Java Code Not Doing What I Want It To Do!

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Location
    Jersey City, New Jersey
    Posts
    10
    My Mood
    Mad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Java Code Not Doing What I Want It To Do!

    The code is supposed to be a timer that stops at the specific minute, second, and hour specified by the user. However, it does not stop and continues without saying anything. This is the code please help!

    import java.util.Scanner;
    public class Timer {
     
    	public int seconds = 0;
    	public int minutes = 0;
    	public int hours = 0;
    	private Scanner hourstop;
    	private Scanner minstop;
    	private Scanner secstop;
     
    	public int counting() throws InterruptedException{
    		for(seconds = 0; ;){
    			seconds++;
    			Thread.sleep(1000);
    			if(seconds >= 60){
    				minutes++;
    				seconds = 0;
    					if(minutes >= 60){
    						hours++;
    						minutes = 0;
    					}
    			}
    			System.out.printf("%02d", hours);
    			System.out.print(":");
    			System.out.printf("%02d", minutes);
    			System.out.print(":");
    			System.out.printf("%02d", seconds);
    			System.out.println(" ");
    		}
    	}
     
    	public boolean timesUp() throws InterruptedException{
    		hourstop = new Scanner(System.in);
    		minstop = new Scanner(System.in);
    		secstop = new Scanner(System.in);
    		System.out.println("Set timer field hour to: ");
    		int x = hourstop.nextInt();
    		System.out.println("Set timer field minutes to: ");
    		int y = minstop.nextInt();
    		System.out.println("Set timer field seconds to: ");
    		int z = secstop.nextInt();
    		boolean bool = false; 
    		if(x == hours && y == minutes && z == seconds)
    		{
    			bool = true; 
    		}
    		if (bool == true)
    		{
    			String TimeisUp = new String("Timer is finished counting");
    			System.out.println(TimeisUp);
    		}
    		System.out.println(counting());
    		return bool; 
    	}
     
    	public static void main(String args[]) throws InterruptedException{
    		Timer t = new Timer();
    			System.out.println(t.timesUp());
    	}
     
    }

    Output:

    //System output
    Set timer field hour to:
    //User Input
    0
    //System output
    Set timer field minutes to:
    //User Input
    0
    //System output
    Set timer field seconds to:
    //User Input
    1
    //System output
    00:00:01
    00:00:02
    00:00:03
    00:00:04
    00:00:05

    Output (different x,y, and z):

    //System output
    Set timer field hour to:
    //User Input
    0
    //System output
    Set timer field minutes to:
    //User Input
    0
    //System output
    Set timer field seconds to:
    //User Input
    0
    //System output
    Timer is finished counting
    00:00:01
    00:00:02
    00:00:03

    Expected Output:

    //System output
    Set timer field hour to:
    //User Input
    0
    //System output
    Set timer field minutes to:
    //User Input
    0
    //System output
    Set timer field seconds to:
    //User Input
    10
    //System output
    00:00:01 //erases this line then prints next line
    00:00:02 //erases this line then prints next line
    00:00:03 //erases this line then prints next line
    00:00:04 //erases this line then prints next line
    00:00:05 //erases this line then prints next line
    00:00:06 //erases this line then prints next line
    00:00:07 //erases this line then prints next line
    00:00:08 //erases this line then prints next line
    00:00:09 //erases this line then prints next line
    Timer is finished counting

    At that the point program would exit.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java Code Not Doing What I Want It To Do!

    Please see the announcements page for the use of code tags when posting code on the forum.

    it does not stop and continues without saying anything
    What did you expect it to say? How do you know it does not stop? Add some printlns to verify what is running and what is not running.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Location
    Jersey City, New Jersey
    Posts
    10
    My Mood
    Mad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Code Not Doing What I Want It To Do!

    It works for 00:00:00 and detects if boolean is true or not and it does print for that time but anything else it wont print the String. I know it does not stop because after it says the string for 00:00:00 it keeps counting up. I want it to stop when it reaches the specified x,y, and z and print the String.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java Code Not Doing What I Want It To Do!

    Please see the link in the previous post for the use of code tags, and edit your post to include code tags. Be sure the formatting is also correct. More people will look at the code and you will potentially get faster/better answers if people can read the code.
    If the code takes input, provide the exact input used and the exact output received from that input, and then post the output you expected instead

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Location
    Jersey City, New Jersey
    Posts
    10
    My Mood
    Mad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Code Not Doing What I Want It To Do!

    I have done what you have said, can you please respond with a solution to this problem?

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java Code Not Doing What I Want It To Do!

    Where do you plan to break out of the infinite loop in the counting method? ..I see no way to exit that loop.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Location
    Jersey City, New Jersey
    Posts
    10
    My Mood
    Mad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Code Not Doing What I Want It To Do!

    When x,y, and z were equal to the hour, minute, and second fields of the timer.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Code Not Doing What I Want It To Do!

    Where is the code that will exit the loop? Can you post the statements to do that?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2013
    Location
    Jersey City, New Jersey
    Posts
    10
    My Mood
    Mad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Code Not Doing What I Want It To Do!

    That is what I need help doing. I want it to exit the loop when it reaches the inputted x,y, and z and say the String!

  10. #10
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Code Not Doing What I Want It To Do!

    Here's a tip I always use while debugging:

    *) Identify the type of error (syntax, logical or run-time).

    Once you have identified the type or error it'll be much easier to find what's not working correctly.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Code Not Doing What I Want It To Do!

    I want it to exit the loop when
    Use an if statement to determine when the conditions have been met and then use a break statement to exit the loop.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: May 23rd, 2013, 04:35 PM
  2. Trouble Porting my Java File Reading Code to Android Code
    By Gravity Games in forum Android Development
    Replies: 0
    Last Post: December 6th, 2012, 04:38 PM
  3. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  4. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM
  5. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM