1

I'm teaching myself how to code with java and I use exercises I find in the Internet to practice what I learn.

I am now in a middle of a question that asks me to compare between two strings(input from the user) and check if the two contain the same letters.

example:

areAnagrams("asd","dsa") -> true
areAnagrams("Debit Card","Bad Credit")=> true

got the idea?

I know that the == check only if them both are refering to the same object. I thought that

public int compareTo(String otherString)

should have done the job. but it doesnt work =\

what i did till now is:

public static boolean areAnagrams(String a, String b)
{
int x=0;
    a.trim();
    b.trim();

    x=a.compareTo(b);
    System.out.println(x);
    return x==0 ? true:false;

}
    public static void main(String[] args)
    {
Scanner temp= new Scanner(System.in);
Scanner temp2= new Scanner(System.in);
String a= temp.next();
String b= temp2.next();
System.out.println(areAnagrams(a,b));

    }

}

but it doesnt work. i think there is a command that should compare value's but i couldnt find it online.

will apriciate your help

thanks!

straiker2
  • 165
  • 1
  • 2
  • 9

3 Answers3

3

How about trying with Arrays like this:-

 char[] w1= firstWord.trim().toUpperCase().replaceAll("[\\s]", "").toCharArray();
 char[] w2= secondWord.trim().toUpperCase().replaceAll("[\\s]", "").toCharArray();
 Arrays.sort(w1);
 Arrays.sort(w2);
 return Arrays.equals(w1, w2);
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • Downcasing or upcasing the words might be in place as well, given the examples presented by the OP. – toniedzwiedz Sep 22 '13 at 15:41
  • Do you remove the white spaces from the strings before adding them to the arrays? I'm not sure if this is good when we check entire idioms. – Kokozaurus Sep 22 '13 at 15:46
2

The compareTo method checks whether the strings are lexicographically equal, see: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#compareTo(java.lang.String)

You want to compare all the characters in the input Strings.

ebo
  • 2,717
  • 1
  • 27
  • 22
  • 2
    This answer is important, because you have misunderstood what compareTo does. This makes me think you haven't looked at the method description in the java api docs, which should always be your first port of call when something doesn't do what you expect it to. – Aaron Sep 22 '13 at 15:43
1

Anagram algorithm in java

In that link, please see:

public static boolean isAnagram(String s1, String s2){

    // Early termination check, if strings are of unequal lengths,
    // then they cannot be anagrams
    if ( s1.length() != s2.length() ) {
        return false;
    }

    char[] c1 = s1.toCharArray();
    char[] c2 = s2.toCharArray();
    Arrays.sort(c1);
    Arrays.sort(c2);
    String sc1 = new String(c1);
    String sc2 = new String(c2);
    return sc1.equals(sc2);

}`

Community
  • 1
  • 1
  • tried to put it in the eclipse but it errored me on the Arrays.sort(c1); line. do i need to import another class? Never mind i found out import java.util.Arrays; :) – straiker2 Sep 22 '13 at 19:39