Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Why does it enter this if ?

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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:

    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 :

    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 !!
    Last edited by Freaky Chris; December 25th, 2011 at 05:53 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

Similar Threads

  1. Trying to start a game when 'Enter' is pressed.
    By Shaybay92 in forum AWT / Java Swing
    Replies: 6
    Last Post: September 27th, 2011, 07:48 AM
  2. Replies: 5
    Last Post: August 11th, 2011, 12:39 PM
  3. i dont know why i cant see the output nor enter input
    By Newbiesss in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 1st, 2011, 12:41 PM
  4. command prompt, acquiring input without enter?
    By kitube in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 24th, 2011, 10:14 PM
  5. How to make user press enter to continue in program?
    By BC2210 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 3rd, 2009, 05:08 AM