Results 1 to 9 of 9
  1. #1
    Globular is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default new to java, trouble with if statement

    Hello everyone, I just started java and I'm having some trouble getting a simple if statement to work properly. Can someone please tell me why this doesn't work right? It's supposed to end the loop if the string variable answer is "YES" but it's not working. Here is my code:

    Java Code:
    import java.util.Scanner;
    
    public class Test {
    	
    	public static void main(String[] args) {
    		
    		Scanner input = new Scanner(System.in);
    		
    		while (true) {
    			
    			System.out.println("What is your first name? ");
    			String firstName = input.nextLine();
    			System.out.println("What is your last name? ");
    			String lastName = input.nextLine();
    			System.out.println();
    			
    			System.out.println(firstName + " " + lastName + ", is this your name, (YES/NO)? ");			
    			String answer = input.nextLine();
    			
    			if (answer == "YES") {
    				break;
    			}
    			else {
    				System.out.println("Please re-enter your information.");
    				System.out.println();
    			}
    		}
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Don't use the comparison operator == to compare objects (yes, Strings are objects). Use the .equals(...) method.

    db

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Don't use the comparison operator == to compare objects (yes, Strings are objects). Use the .equals(...) method.

    db

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by DarrylBurke View Post
    Don't use the comparison operator == to compare objects (yes, Strings are objects). Use the .equals(...) method.

    db
    I think it's not that I am the only one with shaky fingers. The new forum software makes its 'submit' button bounce.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    If you use == to compare objects it will always return false unless you are comparing obj1 with obj1.

    ob1 == obj1 ->(True!)
    obj1 == obj2 ->(False!)

    .equals() will compare the two and see if their values are the same.

    String obj1 = "Woo";
    String obj2 = "Woo";

    obj1.equals(obj2) ->(True!)
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    Quote Originally Posted by Dark View Post
    If you use == to compare objects it will always return false unless you are comparing obj1 with obj1.

    ob1 == obj1 ->(True!)
    obj1 == obj2 ->(False!)

    .equals() will compare the two and see if their values are the same.

    String obj1 = "Woo";
    String obj2 = "Woo";

    obj1.equals(obj2) ->(True!)
    Except in your second case obj1 == obj2 would also return true.

    Strings are not a good thing to use as examples for this.
    :)

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Except in your second case obj1 == obj2 would also return true.

    Strings are not a good thing to use as examples for this.
    :)
    Neither are Integers, nor Longs, nor Shorts, nor Characters, nor Bytes, nor Doubles, nor Floats ... darn, almost nothing can be used as a good example ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    If you use == to compare objects it will always return false
    You can't get at an object except via a reference variable. You can have as many reference variables as you want point to the same object. In which case the values of the reference variables would be the same and == would return true.
    So objRef1 == objRef2 and objRef2 == objRef3 etc can all be true.

    I don't know how to compare the contents of objects other than using a method and passing a reference to the other object to it.

  9. #9
    Globular is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Woah, thanks folks. I had forgotten that strings in Java are in fact objects. The only other language I know is Python and you can use == to compare strings in that. Changing it to answer.equals("YES") fixed it right up. :)

Similar Threads

  1. Trouble installing JAVA
    By ranjiths in forum New To Java
    Replies: 3
    Last Post: 05-07-2011, 03:14 PM
  2. Font trouble in Java 2D
    By eskiom in forum Java 2D
    Replies: 1
    Last Post: 10-24-2010, 08:29 PM
  3. About Trouble shoot in java
    By senthil in forum New To Java
    Replies: 3
    Last Post: 08-31-2009, 06:35 PM
  4. Trouble with Dr Java.
    By davefanelli in forum New To Java
    Replies: 1
    Last Post: 10-22-2008, 06:20 PM
  5. Having trouble with Java Project
    By raakesh in forum New To Java
    Replies: 7
    Last Post: 06-05-2008, 06:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •