Why does it enter this if ?
I am working to make a kind of paint. I got stuck to something that i do not understand. Look here the fragment of code which is making problem:
Code java:
System.out.println("forma: " + fp.forma);
if(fp.forma == "pencil");
{
System.out.println("i entered here");
System.out.println((fp.forma == "pencil"));
x1 = e.getX();
y1 = e.getY();
}
So, if the string fp.forma is set to be "oval" (to draw an oval), at the printlns of this part of code, if get :
Quote:
forma: oval
i entered here
false
So, the strange thing that fp.forma = oval, and the if should run if fp.forma == "pencil"!! As in the println System.out.println((fp.forma == "pencil")); that satement is false !!! Why does this if start if the statement is false ? I tried to replace == with equals. and i get the same thing !! Why ? This if is inside the public void mousePressed(MouseEvent e){}, and because this if runs even if i try to draw an oval, it makes that my program not to work properly !!
Re: Why does it enter this if ?
You should use the equals() method for comparing Strings.
If you think the computer isn't doing what you want or expect your code to do, look again at your code.
The computer is pretty reliable. It must be your code that is wrong.
Remember that ; are used to end statements. Does the if statement go beyond the ; at the end of the line?