Really simple comparision

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sesstreets
    New Member
    • Feb 2008
    • 1

    Really simple comparision

    If I have the variable choice being inputted by the user, how can I use a non case sensitive comparison?

    Basically without doing this:
    [PHP]
    choice = "a";

    if(choice == "a" || choice == "A")
    {...}[/PHP]
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sesstreets
    If I have the variable choice being inputted by the user, how can I use a non case sensitive comparison?

    Basically without doing this:
    [PHP]
    choice = "a";

    if(choice == "a" || choice == "A")
    {...}[/PHP]
    Always use the .equals method for comparing strings in Java.
    There's also an equalsIgnoreCas e method.

    Comment

    • hirak1984
      Contributor
      • Jan 2007
      • 316

      #3
      Originally posted by r035198x
      Always use the .equals method for comparing strings in Java.
      There's also an equalsIgnoreCas e method.
      but in that case, you have to override the equals method of the Object class for creating a meaningful comparison.

      If you use the default ".equals" method of the Object, you are geting the same "==" comparison only.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by hirak1984
        but in that case, you have to override the equals method of the Object class for creating a meaningful comparison.
        ...
        There's no need to override anything. Like I said in my last response there is an equals and an equalsIgnoreCas e method.


        Originally posted by hirak1984
        If you use the default ".equals" method of the Object, you are geting the same "==" comparison only.
        That is not true. == compares equality of object references.
        Try it with some code and give two different references the same string value. == will be false but .equals will be true.
        Read about it in Sun's Java tutorial.

        Comment

        • hirak1984
          Contributor
          • Jan 2007
          • 316

          #5
          Originally posted by r035198x
          That is not true. == compares equality of object references.
          Try it with some code and give two different references the same string value. == will be false but .equals will be true.
          Read about it in Sun's Java tutorial.
          well I tried something like the following:
          Code:
          		String one = "abc";
          		String two = "abc";
          		System.out.println(one==two);
          		System.out.println(one.equals(two));
          		System.out.println(one=="abc");
          		System.out.println(one.equals("abc"));
          		System.out.println(two=="abc");
          		System.out.println(two.equals("abc"));

          and all resulted "true"
          in my eclipse.
          can you mention, in which case, "==" and ".equals" will return different result?

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Change your first line to:

            [code=java]
            String one= new String("abc");
            [/code]
            ... and run your code again.

            kind regards,

            Jos

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              Back to the original question, you could use the String.toLowerC ase (or String.toUpperC ase) method.
              Example:
              [CODE=java]
              String one = new String("abc");
              String two = new String("ABC");
              if(one.toLowerC ase.equals(two. toLowerCase))
              {
              // It will come here, as both are "abc" in lower case
              }
              [/CODE]Greetings,
              Nepomuk

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                Originally posted by nepomuk
                Back to the original question, you could use the String.toLowerC ase (or String.toUpperC ase) method.
                Example:
                [CODE=java]
                String one = new String("abc");
                String two = new String("ABC");
                if(one.toLowerC ase.equals(two. toLowerCase))
                {
                // It will come here, as both are "abc" in lower case
                }
                [/CODE]Greetings,
                Nepomuk
                method equalsIngoreCas e combines that into a single method.

                Comment

                Working...