CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2006
    Posts
    29

    string comparison?

    i know this a stupid question, but i don't why this code isn't working:

    fct = JOptionPane.showInputDialog("enter a string");
    JOptionPane.showMessageDialog(null,fct,"info",JOptionPane.INFORMATION_MESSAGE);
    if (fct == "abc")
    JOptionPane.showMessageDialog(null,"OK","info",JOptionPane.INFORMATION_MESSAGE);

    plz help, i'm new in java

  2. #2
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: string comparison?

    Hi

    For comparing Strings you need to use the equals() method ie:
    Code:
    String a = "123";
    String b = "123";
    
    if(a.equals(b)){
    
    }
    This is because == checks if you are working with the same object reference, it doesn't check the values of the Strings themselves.

    Hope This Helps
    Byron Tymvios

    Please use [ CODE ] and [/ CODE ] tags when posting code! See THIS on how to use code tags.

  3. #3
    Join Date
    Sep 2006
    Posts
    29

    Re: string comparison?

    thanks a lot, sure you helped
    and thanks for the extra infos about the object reference comparison

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured