I am using Eclipse to right there It is coming up with an error within the switch statement saying that the Clock.* has not been initialized. it says Description "The local variable Clock may not have been initialized" but doesn't that happen when i do "Stopwatch Clock;" I am still getting used to Java. I am taking some C++ classes and i was trying to find out if i can this to work in java like i did in C++

public class start {
	static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		int a=1;
		int c=0;

		while(c!=5) {
			System.out.print("1-Start, 2-Stop, 3-Reset, 4");
			System.out.println("-Display, 5-Quit");
			c=sc.nextInt();
			Stopwatch Clock;
			switch(c) {
				
				case 1: Clock.Start();break;
				case 2: Clock.Stop();break;
				case 3: Clock.Reset();break;
				case 4: Clock.Display();break;
			} //end of switch for calling stopwatch
		}//end of while loop once int 5 comes in
	}//end of main
}//end of class

Stopwatch.java

public class Stopwatch {
    private //vars go here.
    
    Stopwatch() {
    	//sets var to =0
    }
//----------------------------------------------------
	public void Start() {
		//start clock
	}
//----------------------------------------------------
	public void Stop() {
		//stop clock
	}
//----------------------------------------------------
	public void Reset() {
	//reset clock
	}
//----------------------------------------------------
	public void Display() {
        //call output for format of dispay
	}
	//----------------------------------------------------
	private void Output() {
		//put clock to screen
	
	}
}

Recommended Answers

All 5 Replies

Where in this part of the code have you defined Clock.

Stopwatch Clock;
			switch(c) {
				
				case 1: Clock.Start();break;
				case 2: Clock.Stop();break;
				case 3: Clock.Reset();break;
				case 4: Clock.Display();break;
			} //end of switch for calling stopwatch

isn't

Stopwatch Clock;

where I define Clock. i know that is how i got it working in C. but I am not sure if that is right for java.

Where in this part of the code have you defined Clock.

Stopwatch Clock;
			switch(c) {
				
				case 1: Clock.Start();break;
				case 2: Clock.Stop();break;
				case 3: Clock.Reset();break;
				case 4: Clock.Display();break;
			} //end of switch for calling stopwatch

It is declared, yes, but it is not initialized

Stopwatch Clock = new Stopwatch();

(It really should be "clock" instead of "Clock". Common convention is for variables to start with a lowercase letter. Classes are capitalized.)

Thank you..... I feel bad now I at one point i did try Stopwatch Clock = new Stopwatch; so i forgot to put the () on StopWatch. I fell bad now. But thank you for the help

Everyone learns at some point :)

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.