After setting the time where it should start, you enter "y".. the timer will start..
It's supposed to stop whenever you want it to by pressing "n"
but I don't know how..

The only way I know how to get it to ask for "y" or "n" again is to set what day it should prompt you..
but that's not what we're supposed to make.. D:

What should I do.. so you won't have to set when it should prompt you to enter "y" or "n"? Is it even possible?
I tried putting the 2nd prompt inside the loop but then it makes it stop each time the time is incremented..****

myClock CLASS

/**
 * @(#)myClock.java
 *
 *
 * @author
 * @version 1.00 2012/5/8
 */

import java.io.*;
import java.util.*;

public class myClock
{
    static Scanner scan = new Scanner (System.in);


    private int hr;
    private int min;
    private int sec;
    private int da;
    private String startStop;







            public myClock()
            {
                setTime(0 , 0, 0 , 0);
            }

            public myClock(int d, int h, int m , int s)
            {
                setTime(d, h , m , s);
            }




             public void setTime(int day , int hour , int minute , int second )
             {
                if (0 <= day )
                    da = day;

                else
                    da = 0;


                if (0 <= hour && hour < 24 )
                    hr = hour;

                else
                    hr = 0;

                if (0 <= minute && minute < 60)
                    min = minute ;

                else
                    min = 0;

                if (0 <= second && second < 60 )
                    sec = second;

                else sec = 0;
             }


                public void AskstartStop()
                {


                    System.out.println ("Enter \"y\" to start clock.");
                    startStop = scan.next();

                }




            public int getDay()
                {return da;}

             public int getHour()
                { return hr;}

            public int setMin()
                {return min; }

            public int getSec()
                {return sec;}

            public void printTime()
            {
                    if(da <= 10)
                        System.out.print("0");
                        System.out.print(da + ":");
                    if(hr < 10)
                        System.out.print("0");
                        System.out.print(hr + ":");
                    if (min <10)
                        System.out.print("0");
                        System.out.print (min + ":");

                    if (sec < 10)
                        System.out.print("0");
                        System.out.print(sec + ":");
            }




            public void incrementSecond()
            {

                while (startStop.equalsIgnoreCase("y")) // AT THE BEGINING AskstartStop() WILL ASK TO ENTER "y" TO START THE TIMER.
                {
                    sec++;                              // THEN THE TIMER STARTS....
                    System.out.println();
                    printTime();


                    if (sec > 59)
                    {
                    sec = 0 ;
                    incrementMinute();
                    }


                    if (startStop.equalsIgnoreCase("n") == true ) // WHERE CAN I PUT ANOTHER PROMPT;
                                                                   //startStop = scan.next();
                                                                   //WHERE THE USER CAN ENTER "n"
                    {System.exit(0);}                           // AND STOP THE TIMER?



                }


            }




            public void incrementMinute()
            {
                min++;
                if ( min > 59)
                {min = 0;

                incrementHour();
                }
            }

            public void incrementHour()
            {
                hr++;
                if (hr > 23)
                    {hr = 0;
                incrementDay();}
            }

            public void incrementDay()
            {
                    da++;

                }



}

myClockTest

import java.io.*;
import java.util.*;

public class myClockTest{


static Scanner setset = new Scanner(System.in);

    public static void main (String[]args) {
        myClock mc = new myClock (00 ,00, 00, 00);

System.out.print( "Set Seconds:");
int secondsSet = setset.nextInt();
System.out.println();
System.out.print( "Set Minutes:");
int minutesSet = setset.nextInt();
System.out.println();
System.out.print( "Set Hours:");
int hoursSet = setset.nextInt();
System.out.println();
System.out.print( "Set Days:");
int daysSet = setset.nextInt();




    mc.AskstartStop();
    mc.printTime();
    mc.setTime( daysSet,hoursSet, minutesSet, secondsSet);
    mc.incrementSecond();
    System.out.println();
    mc.printTime();




    }}

Edited 4 Years Ago by Johannady2

if you want this to work you will need to execute the loop on a different thread, otherwise you are never going to be able to ask for a user prompt to stop the clock while the infinite loop is going.

even then im not sure how you are going to be able to prompt user input from a console window while you have an infinite loop printing stuff, even if its on a different thread... It would be much easier with a basic graphic ui but im guessing your class isn't there yet...

what exactly does the assignement say about how to stop the clock from running?

on a side note, if you want your seconds to closer to real seconds, try adding this bit of code to your loop :

try {
    //thread to sleep for the specified number of milliseconds
    Thread.sleep(1000);
} catch (Exception e) {
    System.out.println(e);
}

You can't use input from the console with threads to do what you want. If you are using the console for any input and are printing messages on the console, it is very difficult for a separate thread to ask a question while using the console for those other things.

Can you explain how you want the program to work?

Well.. it's not important anymore since I already submitted the homework and it was wrong! XD
and our teacher said.. we won't be studying Threads until we take our OS subject...

The program should stop while the time is running......
Like stop it any time you want. by pressing "n" (or whatever you want) ...

Actually no one was able to do that. I dunno why our teacher said we should make that.. I guess it didn't come into his mind that we need threads.

well that is kinda weird if you ask me, any teacher should be able to do his own homeworks, and by the way you describe it, i dont think that was possible. :(

You should be able to do it by checking System.in.available() inside the timer's loop each second to see if the user has typed anything. If he has then you can use a next... to see what that input was, and exit the timer loop as appropriate. Unlike next... available() does not block your execution waiting for input.
(Just an idea)

This article has been dead for over six months. Start a new discussion instead.