How frustrating... I get to the end of something that looks as if it should go through the compiler ok, but it doesn't. I'm trying to make a sort-of stop watch that counts in nanoseconds. What did I do wrong, or what am I missing now?

public class Counter
{
	long startTime;
	long endTime;

	public void Stopwatch()
	{
	long currentTime = System.nanoTime();
	startTime = currentTime;
	endTime = currentTime;
	}

	public void start()
	{
	startTime = System.nanoTime();
	}

	public void stop()
	{
	endTime = System.nanoTime();
	}

	public long getTimeElapsed()
	{
	return (endTime - startTime);
	}

}

Recommended Answers

All 9 Replies

That's why the compiler gives you those handy messages when there are problems :)

What do they say?

That's why the compiler gives you those handy messages when there are problems :)

What do they say?

Actually, I'm sorry, it's not the compiler that's having the issue - the program just craps out when I go to run it. It says, "Exception in thread java.lang.NoSuchMethodError: main"

What did I miss/do incorrectly?

Just what it says - you have no main() method defined.
Also, your class is called Counter, but it looks like you want this method public void Stopwatch() to be a constructor? Or am I misunderstanding that?

I think that's right Ezzaral.
If it *is* a constructor, you don't have to include void as a return type, since it's normal behaviour for a constructor to return an object of its class.

Now onto that main method. Every program needs a main method so Java knows where to start. (Much like a websites starting page is index.html or something.)

The syntax of this main method is as follows:

public static void main(String[] args) {
   // your code here
}

The args array contains the arguments that are given to the project when started from the command line.

Black Box

I know, I just make things really difficult (on myself, of course), because I forget stupid things here or there... Like now, I'm a bit lost on what I should be doing. I corrected the one aspect (main method), but I'm still having troubles:

public class Stopwatch
{

	public static void main(String[] args)
	{

		long startTime;
		long endTime;

		public Stopwatch()
		{
		long currentTime = System.nanoTime();
		startTime = currentTime;
		endTime = currentTime;
		}

		public start()
		{
		startTime = System.nanoTime();
		}

		public stop()
		{
		endTime = System.nanoTime();
		}

		public getTimeElapsed()
		{
		return (endTime - startTime);
	}

}

Should I be putting something in between public and the methods, in order to get it to work properly?

You don't want to put all of the class code in main. Main just needs to create a Stopwatch object and call the methods on it.

Ok... I guess I'm just not understanding. You don't have to give me "The" code, but can someone please at least give me an example that I can apply to this, so I can finally finish it, and understand how it works?

Many thanks again

You have moved ALL of your class code into the main() method. You cannot nest all of that into main. It needs to remain part of the class. Your main method is just a driver to create a Stopwatch object and then call the methods on that object.

Yes, that whole main thing is just another method, only a special one which Java pays attention to. You don't define methods in methods, you can call them naturally.

For example:

class MyClass {

    //constructor
    public MyClass() {
        //do construction work.
    }

    // a method
    public void myMethod() {
        // do work.
    }

    //the special main method.
    public static void main(String[] args) {
        MyClass myclassObject = new MyClass();
        myclassObject.myMethod();
    }

}

Black Box

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.