Hello,

I'm having trouble displaying an output from my code. It suppose to act like a stopwatch where seconds increment and then minutes and then hours but for some reason its not working and I cannot figure out why... :-( hope some some can guide me.

Thanks

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package stopwatch;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

/**
 *
 * @author Taimoor
 */
public class stopWatch
{
    int seconds = 0;
    int minutes = 0;
    int hours = 0;
    boolean t = true;
    Timer timer;

    public stopWatch()
    {
        timer = new Timer(1000,new TimerClass());
        timer.start();
        while(t)
        {
            System.out.print(seconds + minutes + hours);
        }
        
    }
    
    class TimerClass implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            seconds+= 1;
            if (seconds > 59)
            {
                seconds = 0;
                minutes += 1;
            }
            if (minutes > 59)
            {
                minutes = 0;
                hours += 1;
            }
            if(hours > 23)
            {
                hours = 0;
            }
        }
    }
    
    public static void main(String[]args)
    {
        stopWatch watch = new stopWatch();
    }
}

>> but for some reason its not working

Define "working". What's it doing that you don't want it to do or vice versa? It looks like it'll keep fine time. You'll get twenty zillion 0's then twenty zillion 1's, then twenty zillion 2's, etc.

I imagine you want to have ONE 0, ONE 1, ONE 2, etc.

So ask yourself "Why is it printing so many numbers? Why isn't it only printing once a second?"

The better question is "What occurs exactly once a second?"

Once you have answered that question, if you want something to execute once a second, put it in the code that executes once a second.

Hey Vernon,

yes, i do want it to print 1,2,3 ...

I run this code but nothing is produced and i cannot figure out it not doing so...

>> nothing is produced

NOTHING is produced? Then the program either isn't compiling, you're not running it correctly, or you're getting a run-time error.

Stick this line right before line 59 and make sure it prints...

System.out.println("Hello World");

If it gets into main at all, that will print.

Actually, I think your problem might possibly be as simple as capitalization. whether you HAVE to use the capitalization like this or are just strongly encouraged to I'm not sure, but if the Operating System or Java gets picky it could be a problem. Try renaming your class to StopWatch (i.e. first letter is a capital). Make sure the file is named StopWatch.java and is in directory stopwatch.

Vernon,

I tried changing the class name to StopWatch.java but still no difference

I added " System.out.println("Hello World");" at line 60 and still no output BUT when I added "System.out.println("Hello World");" to line 58 the out put was "Hello World".

Vernon,

I tried changing the class name to StopWatch.java but still no difference

I added " System.out.println("Hello World");" at line 60 and still no output BUT when I added "System.out.println("Hello World");" to line 58 the out put was "Hello World".

So you get exactly one Hello World displayed, no other output at all, and there are no runtime errors? That doesn't make any sense to me. I ran your program (with the changed name) and got what I described in my first post (tons of 0's tons of 1's, tons of 2's, etc.). You get a Hello World, but you don't get any 0's or 1's or 2's?

Yes I get no output, except ONE "Hello World". :-(

Thank you anyways :-)

Your program runs just like Vernon described. You are using Netbeans IDE and probably you think that it is not doing anything because the output window does not show anything. That is because your output is infinitely long. Try pressing the 'stop building' button on the left of the output window. You'll probably see the output (as much as the window could display). Your actual problem is as Vernon described.

I got it to work guys, Thank you to both of you :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.