Results 1 to 8 of 8
Like Tree3Likes
  • 1 Post By DarrylBurke
  • 1 Post By Junky
  • 1 Post By Dark

Thread: String If Statements

  1. #1
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default String If Statements

    Hello, I have recently encountered a problem where I'm not sure exactly how to do an if statement in check for strings. I do know how to check for numbers just for some reason I can't seem to figure this out.

    I thought it would be something like
    if(fn == "hello){
    System.out.println("Whats Up?");

    }

    but that didn't work

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    This is one of the most common problems encountered by n00bs and one the most often asked questions. Use the equals method instead. Do a search and find the 1000000000 times it has been explained.

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Use code tags when posting codes.

    As what junky said use equals() method:
    Java Code:
        if("hello".equals(aString))
        {
            //your code
        }

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

    Default

    Quote Originally Posted by Junky View Post
    This is one of the most common problems encountered by n00bs and one the most often asked questions. Use the equals method instead. Do a search and find the 1000000000 times it has been explained.
    You missed a few zeros

    db
    Dark likes this.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    My finger got tired!
    DarrylBurke likes this.

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

    Default

    Quote Originally Posted by Junky View Post
    My finger got tired!
    You could have just held the key down
    DarrylBurke likes this.
    • 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.

  7. #7
    Join Date
    Mar 2010
    Posts
    26
    Rep Power
    0

    Default

    if(fn.equals("hello"){
    System.out.println("Whats Up?");
    }

    this code is work because == is used for same object but two reference are pointing to same object that time use == operator.

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

    Default

    To clarify what jatinkansagara said, you would use the == operator to test if two object references reference the same object. See example.

    Java Code:
    class MyProgram
    {
         public static void main(String[] args)
         {
              Object foo1 = new Object();
              Object foo2 = new Object();
              Object foo3 = foo1;
              
              if(foo1 == foo3)
                  System.out.println("Foo1 does equal Foo3!");
              if(foo1 == foo2)
                  System.out.println("Foo1 does equal Foo2!");
              else
                  System.out.println("Foo1 does not equal Foo2!");
         }
    }
    • 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.

Similar Threads

  1. Help with if else statements
    By np2392 in forum New To Java
    Replies: 2
    Last Post: 09-24-2010, 01:25 AM
  2. if else statements
    By sweetpea123 in forum New To Java
    Replies: 4
    Last Post: 04-12-2010, 07:02 PM
  3. age: using if statements
    By yasmin k in forum New To Java
    Replies: 2
    Last Post: 10-04-2009, 09:50 PM
  4. Help with if-else statements
    By porchrat in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 04:24 PM
  5. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 07:56 PM

Posting Permissions

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