3

Why does this string comparison fail?

package javaapplication57;

/**
*
* @author amit
*/
public class JavaApplication57 {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
        String a = "anagram";
        String b = "margana";
        int len = a.length();
        char[] temp = a.toCharArray();
        char[] temp2 = b.toCharArray();
        int len2 = b.length();

        for(int j = 0; j<len-1; j++)
        {    
            for(int i = 0; i<len-1; i++)
            {
                if(temp[i]>temp[i+1])
                {
                    char t = temp[i];
                    temp[i] = temp[i+1];
                    temp[i+1] = t;
                }
            }
        }
        //System.out.println(temp);

        for(int j = 0; j<len2-1; j++)
        {    
            for(int i = 0; i<len2-1; i++)
            {
                if(temp2[i]>temp2[i+1])
                {
                    char t = temp2[i];
                    temp2[i] = temp2[i+1];
                    temp2[i+1] = t;
                }
            }
        }
        //System.out.println(temp2);
        if(temp.equals(temp2))
        {
            System.out.println("yes");
        }
    }

}
4
  • 1
    Please explain what you mean by "fail" -- what do you expect this code to do that it's not doing? Commented Jul 27, 2017 at 12:12
  • 2
    temp and temp2 are char[] and using equals between them does not work (it does not do what you think it does). Try this instead: if (Arrays.equals(temp, temp2)) Commented Jul 27, 2017 at 12:13
  • What exactly is the problem? Where does it fail? What does your code do? What is it supposed to do? Commented Jul 27, 2017 at 12:14
  • @jesper we might aswell also mention that calling equals on arrays check if they are the same array and not two different, but identical, arrays. Commented Jul 27, 2017 at 12:15

3 Answers 3

6

You are not comparing Strings. You are comparing arrays of char. Arrays don't override Object's equals, so equals behaves the same as ==. You can create Strings from the arrays you wish to compare and run equals on the resulting Strings.

if(new String(temp).equals(new String(temp2)))
Sign up to request clarification or add additional context in comments.

Comments

2

just use the .equals method from java

String a = "anagram";
String b = "margana";
if(a.equals(b){
    System.out.pring("equals");
}else{
    System.out.pring("not equals";
}

Comments

1

Assuming you are not using built in methods, you have to first check for a case: The length of both strings have to be equal. No need to use a char array just go through both strings, after checking if lengths are equal.

public static boolean equals(String str, String str2){

      if(str.length() != str2.length()){
         return false;
      }
      for(int i=0; i<str.length(); i++){
         if(str.charAt(i) != str2.charAt(i)){
            return false;
         }
      }
      return true;
   }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.