Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java won't compile

Options
  • 12-11-2008 10:40am
    #1
    Registered Users Posts: 342 ✭✭


    Hi can somebody please help me,i keep getting the error message that variable start might not have been initialized...here's the program,any help appreciated

    import graphics.*;
    
    public class ReactionTester   //This program is like a game and measures how long it takes the user to click a circle that appears at a random position
    {
    	public static void main(String [] args)
    	{
    		Window win = new Window(750,750);   //This is the size of the box that the program will be in
    		long start;
    		long stop;
    		int i = (int)(500 * Math.random());   //This method is used to generate the position of the circle
    		int r = (int)(75 * Math.random());   //This method is used to generate the size of the circle
    		int answer;
    		Circle RandomCircle = new Circle(i,i,r);   //This is the method for the circle and what creates the circle
    		win.draw(RandomCircle);   //This draws the random circle
    		long time;
    
    
    		while(true)   //This makes the program wait for the user to click the mouse
    		{
    
    			if(win.isPressed())   //This is will happen after the first click
    
    			{
    				start = System.currentTimeMillis();   //This remembers the current time
    			}
    			if(win.isPressed())   //This will happen after the second click
    			{
    				stop = System.currentTimeMillis();   //This remembers the current time
    
    				Point mouse = win.getMouse();   //This gets the position of the mouse
    
    				int x1= mouse.getX();   //This is the x position
    
    				int y1 = mouse.getY();   //and this is the y position
    
    				answer=(((x1-i)*(x1-i))+((y1-i)*(y1-i)));   //This works out if the mouse was clicked inside the circle
    
    
    
    				if(answer<=(r*r))   //This will happen if they have clicked inside the circle
    
    				{
    
    					win.erase(RandomCircle);   //This erases the circle
    					System.out.println("It took " + (stop - start) + " milliseconds to click the circle");   //This prints off how long it took to click the circle
    
    				}
    
    				else if(answer>(r*r))   //This will happen if they havent clicked inside the circle
    
    				{
    
    					win.draw(RandomCircle);   //It just draws another random circle and starts again
    
    				}
    			}
    		}
    	}
    }
    


Comments

  • Registered Users Posts: 610 ✭✭✭nialo


    set your long values to zero.


  • Registered Users Posts: 1,996 ✭✭✭lynchie


    long is auto-defaulted to 0 by the compiler so thats not the issue.

    It should not cause a compiler error either way, its only a warning and would only occur if start was of type java.lang.Long.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Are you sure that it not just a warning. It should be able to compile with this warning unless you've the compiler set to a strict mode where it won't even compile unless all errors removed.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    lynchie wrote: »
    long is auto-defaulted to 0 by the compiler so thats not the issue.

    That is not true.

    Variables will only auto-default if the variable is within the class scope. Variables that are defined within methods must be assigned a value at definition time.

    start, stop, answer and time all need a default value or move them into the class rather then the method.

    "might not have been initialized" is a warning if I recall correctly. Not an error.


  • Registered Users Posts: 1,996 ✭✭✭lynchie


    Hobbes wrote: »
    That is not true.

    Variables will only auto-default if the variable is within the class scope. Variables that are defined within methods must be assigned a value at definition time.

    start, stop, answer and time all need a default value or move them into the class rather then the method.

    "might not have been initialized" is a warning if I recall correctly. Not an error.

    Yeah thinking about it again ur right.. Only class level variables auto initialise. So unless those 3 variables are initialised or moved to class level, the compiler will actually throw a compile time error.


  • Advertisement
  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    as far as I remember as well the compiler will be smart about it.

    So if you initialised it at a later point the warning won't show up. But if it is put into a conditional section and not all sections contain an initialiser then it will throw up the error.


  • Registered Users Posts: 342 ✭✭thomasjad


    I fixed that problem but now i have two more...firstly it keeps saying that it took o milliseconds to click the circle,secondly if you click outside the circle it's supposed to draw a new random circle but it draws the exact same one in the same position,any suggestions?


  • Registered Users Posts: 610 ✭✭✭nialo


    RandomCircle on the second write is the exact same. you never reinitialise it with new values. It wont do this on its own...


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    you should name RandomCircle as randomCircle. variables start with a lowercase, classes with an Uppercase.

    and what nialo said is correct.

    not sure about the zero millisecond. It could be that System.out.println is converting them to an integer.

    try doing the maths outside of the println and then just print the result.


  • Registered Users Posts: 342 ✭✭thomasjad


    Emm i kinda new to programming,how do you reinitialise it?:confused:


  • Advertisement
  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    i = (int)(500 * Math.random())
    r = (int)(75 * Math.random())
    RandomCircle = new Circle(i,i,r);

    Although you would be better off putting this into a method so all you need to do is..

    randomCircle = Circle.getRandomCircle();


  • Registered Users Posts: 342 ✭✭thomasjad


    I have this code
    long test = stop - start;
    System.out.println("It took " + (test) + " milliseconds to click the circle");
    
    in it to print out how long it takes to click the circle but it keeps giving me loads of results in terminal...
    It took 4109 milliseconds to click the circle
    It took 4120 milliseconds to click the circle
    It took 4120 milliseconds to click the circle
    It took 4121 milliseconds to click the circle
    It took 4121 milliseconds to click the circle
    It took 4122 milliseconds to click the circle
    It took 4122 milliseconds to click the circle
    It took 4123 milliseconds to click the circle
    It took 4123 milliseconds to click the circle
    It took 4124 milliseconds to click the circle
    It took 4125 milliseconds to click the circle
    It took 4127 milliseconds to click the circle
    It took 4146 milliseconds to click the circle
    It took 4147 milliseconds to click the circle
    It took 4147 milliseconds to click the circle
    It took 4149 milliseconds to click the circle
    It took 4150 milliseconds to click the circle
    It took 4151 milliseconds to click the circle
    It took 4151 milliseconds to click the circle
    It took 4152 milliseconds to click the circle
    It took 4152 milliseconds to click the circle
    It took 4153 milliseconds to click the circle
    It took 4153 milliseconds to click the circle
    It took 4155 milliseconds to click the circle
    It took 4156 milliseconds to click the circle
    It took 4156 milliseconds to click the circle
    It took 4157 milliseconds to click the circle
    It took 4157 milliseconds to click the circle
    It took 4160 milliseconds to click the circle
    It took 4160 milliseconds to click the circle
    It took 4161 milliseconds to click the circle
    It took 4161 milliseconds to click the circle
    It took 4162 milliseconds to click the circle
    It took 4162 milliseconds to click the circle
    It took 4163 milliseconds to click the circle
    It took 4163 milliseconds to click the circle
    It took 4164 milliseconds to click the circle
    It took 4165 milliseconds to click the circle
    It took 4165 milliseconds to click the circle
    It took 4176 milliseconds to click the circle
    It took 4176 milliseconds to click the circle
    It took 4177 milliseconds to click the circle
    It took 4181 milliseconds to click the circle
    It took 4181 milliseconds to click the circle
    It took 4182 milliseconds to click the circle
    It took 4182 milliseconds to click the circle
    It took 4183 milliseconds to click the circle
    It took 4183 milliseconds to click the circle
    It took 4184 milliseconds to click the circle
    It took 4184 milliseconds to click the circle
    It took 4184 milliseconds to click the circle
    It took 4185 milliseconds to click the circle
    It took 4185 milliseconds to click the circle
    It took 4186 milliseconds to click the circle
    It took 4186 milliseconds to click the circle
    It took 4187 milliseconds to click the circle
    It took 4187 milliseconds to click the circle
    It took 4188 milliseconds to click the circle
    It took 4188 milliseconds to click the circle
    It took 4188 milliseconds to click the circle
    It took 4189 milliseconds to click the circle
    It took 4189 milliseconds to click the circle
    It took 4190 milliseconds to click the circle
    It took 4191 milliseconds to click the circle
    It took 4191 milliseconds to click the circle
    It took 4191 milliseconds to click the circle
    It took 4192 milliseconds to click the circle
    It took 4192 milliseconds to click the circle
    It took 4193 milliseconds to click the circle
    It took 4193 milliseconds to click the circle
    It took 4194 milliseconds to click the circle
    It took 4194 milliseconds to click the circle
    It took 4195 milliseconds to click the circle
    It took 4195 milliseconds to click the circle
    It took 4196 milliseconds to click the circle
    It took 4197 milliseconds to click the circle
    It took 4197 milliseconds to click the circle
    It took 4198 milliseconds to click the circle
    It took 4198 milliseconds to click the circle
    It took 4198 milliseconds to click the circle
    It took 4199 milliseconds to click the circle
    It took 4199 milliseconds to click the circle
    It took 4200 milliseconds to click the circle
    It took 4200 milliseconds to click the circle
    It took 4309 milliseconds to click the circle
    It took 4310 milliseconds to click the circle
    It took 4310 milliseconds to click the circle
    It took 4311 milliseconds to click the circle
    It took 4311 milliseconds to click the circle
    It took 4312 milliseconds to click the circle
    It took 4312 milliseconds to click the circle
    It took 4313 milliseconds to click the circle
    It took 4313 milliseconds to click the circle
    It took 4314 milliseconds to click the circle
    It took 4314 milliseconds to click the circle
    It took 4414 milliseconds to click the circle
    It took 4415 milliseconds to click the circle
    It took 4416 milliseconds to click the circle
    It took 4417 milliseconds to click the circle
    It took 4417 milliseconds to click the circle
    It took 4418 milliseconds to click the circle
    It took 4418 milliseconds to click the circle
    It took 4419 milliseconds to click the circle
    It took 4419 milliseconds to click the circle
    It took 4420 milliseconds to click the circle
    It took 4420 milliseconds to click the circle
    It took 4520 milliseconds to click the circle
    It took 4521 milliseconds to click the circle
    It took 4522 milliseconds to click the circle
    It took 4522 milliseconds to click the circle
    It took 4523 milliseconds to click the circle
    It took 4524 milliseconds to click the circle
    It took 4524 milliseconds to click the circle
    It took 4524 milliseconds to click the circle
    It took 4524 milliseconds to click the circle
    It took 4524 milliseconds to click the circle
    It took 4525 milliseconds to click the circle
    It took 4525 milliseconds to click the circle
    It took 4525 milliseconds to click the circle
    It took 4525 milliseconds to click the circle
    It took 4525 milliseconds to click the circle
    It took 4525 milliseconds to click the circle
    It took 4525 milliseconds to click the circle
    It took 4525 milliseconds to click the circle
    It took 4525 milliseconds to click the circle
    It took 4525 milliseconds to click the circle
    It took 4525 milliseconds to click the circle
    It took 4525 milliseconds to click the circle
    It took 4526 milliseconds to click the circle
    It took 4526 milliseconds to click the circle
    It took 4526 milliseconds to click the circle
    It took 4526 milliseconds to click the circle
    It took 4526 milliseconds to click the circle
    It took 4526 milliseconds to click the circle
    It took 4526 milliseconds to click the circle
    It took 4526 milliseconds to click the circle
    It took 4526 milliseconds to click the circle
    It took 4526 milliseconds to click the circle
    It took 4526 milliseconds to click the circle
    It took 4526 milliseconds to click the circle
    It took 4526 milliseconds to click the circle
    It took 4527 milliseconds to click the circle
    It took 4527 milliseconds to click the circle
    It took 4527 milliseconds to click the circle
    It took 4528 milliseconds to click the circle
    It took 4528 milliseconds to click the circle
    It took 4528 milliseconds to click the circle
    It took 4528 milliseconds to click the circle
    It took 4528 milliseconds to click the circle
    It took 4528 milliseconds to click the circle
    It took 4529 milliseconds to click the circle
    It took 4529 milliseconds to click the circle
    It took 4529 milliseconds to click the circle
    It took 4529 milliseconds to click the circle
    It took 4529 milliseconds to click the circle
    It took 4529 milliseconds to click the circle
    It took 4529 milliseconds to click the circle
    It took 4529 milliseconds to click the circle
    It took 4532 milliseconds to click the circle
    It took 4533 milliseconds to click the circle
    It took 4533 milliseconds to click the circle
    It took 4533 milliseconds to click the circle
    It took 4533 milliseconds to click the circle
    It took 4533 milliseconds to click the circle
    It took 4533 milliseconds to click the circle
    It took 4533 milliseconds to click the circle
    It took 4533 milliseconds to click the circle
    It took 4533 milliseconds to click the circle
    It took 4533 milliseconds to click the circle
    It took 4534 milliseconds to click the circle
    It took 4534 milliseconds to click the circle
    It took 4534 milliseconds to click the circle
    It took 4534 milliseconds to click the circle
    It took 4603 milliseconds to click the circle
    It took 4677 milliseconds to click the circle
    It took 4677 milliseconds to click the circle
    It took 4677 milliseconds to click the circle
    It took 4678 milliseconds to click the circle
    It took 4678 milliseconds to click the circle
    It took 4678 milliseconds to click the circle
    It took 4678 milliseconds to click the circle
    It took 4679 milliseconds to click the circle
    It took 4679 milliseconds to click the circle
    It took 4679 milliseconds to click the circle
    It took 4679 milliseconds to click the circle
    It took 4679 milliseconds to click the circle
    It took 4679 milliseconds to click the circle
    It took 4679 milliseconds to click the circle
    It took 4679 milliseconds to click the circle
    It took 4679 milliseconds to click the circle
    It took 4679 milliseconds to click the circle
    It took 4679 milliseconds to click the circle
    It took 4679 milliseconds to click the circle
    It took 4679 milliseconds to click the circle
    It took 4680 milliseconds to click the circle
    It took 4680 milliseconds to click the circle
    It took 4680 milliseconds to click the circle
    It took 4680 milliseconds to click the circle
    It took 4680 milliseconds to click the circle
    It took 4680 milliseconds to click the circle
    It took 4680 milliseconds to click the circle
    It took 4680 milliseconds to click the circle
    It took 4966 milliseconds to click the circle
    It took 4967 milliseconds to click the circle
    It took 4967 milliseconds to click the circle
    It took 4967 milliseconds to click the circle
    It took 4967 milliseconds to click the circle
    It took 4967 milliseconds to click the circle
    It took 4967 milliseconds to click the circle
    It took 4967 milliseconds to click the circle
    It took 4968 milliseconds to click the circle
    


  • Registered Users Posts: 610 ✭✭✭nialo


    Looks like your while loop is just looping without stop after your first click. Would suggest that you need a break condition to pause the loop and to wait for the next click. cant be 100% sure what win.isPressed() does thou.


  • Registered Users Posts: 6,240 ✭✭✭hussey


    the problem is these lines
    if([B]win.isPressed()[/B])   //This is will happen after the first click
    			{
    				[B]start [/B]= System.currentTimeMillis();   //This remembers the current time
    			}
    			if([B]win.isPressed()[/B])   //This will happen after the second click
    			{
    				[B]stop [/B]= System.currentTimeMillis(); 
    

    so you have two different loops, the compiler doesn't know that these are the same condition, and therefore doesn't know that win.isPressed() occurs that both variables will be initialised.
    so my question is why have them in different loops?

    the reason why it is in an infinite loop is I assume you never reset the win.isPressed() (?) so this is always true after the first time,

    could it be possible to use a listener? When an action occurs the listener will activate and do something??


  • Registered Users Posts: 342 ✭✭thomasjad


    That was my original were I had it that it would be for a double click,i got rid of the outer if(win.isPressed) and it works well now.thank u all


Advertisement