开发者

Odd comparison problem in checking for anagram

开发者 https://www.devze.com 2022-12-14 08:47 出处:网络
I\'m sorry, the title\'s awful; however, I couldn\'t think of any better way to summarize my plight. In trying to solve a problem involving checking to see if one string is an anagram of another, I i

I'm sorry, the title's awful; however, I couldn't think of any better way to summarize my plight.

In trying to solve a problem involving checking to see if one string is an anagram of another, I implemented a solution that involved removing all whitespace from both strings, converting them both to character arrays, sorting them and then seeing if they are equal开发者_高级运维 to eachother.

If so, the program prints out "Is an anagram.", otherwise "Is not an anagram."

The problem is that even though my code compiles successfully and runs fine, the end result will always be "Is not an anagram.", regardless of whether or not the two original strings are indeed anagrams of each other. Quick code I inserted for debugging shows that, in a case with an actual anagram, the two character arrays I end up comparing are apparently identical, yet the result of the comparison is false.

I can't tell why exactly this is happening, unless I'm overlooking something incredibly obvious or there are some extra undisplayed characters in what I compare.

Here's the code:

import java.util.Arrays;
import java.util.Scanner;
public class Anagram {
    public static void main(String[] args) {
        char[] test1;
        char[] test2;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter first phrase>");
        test1 = input.nextLine().replaceAll(" ", "").toCharArray();
        Arrays.sort(test1);
        System.out.print("Enter second phrase>");
        test2 = input.nextLine().replaceAll(" ", "").toCharArray();
        Arrays.sort(test2);
        if (test1.equals(test2)) {
            System.out.println("Is an anagram.");
        }
        else {
            System.out.println("Is not an anagram.");
        }
        /* debugging */
        System.out.println(test1);
        System.out.println(test2);
        System.out.println(test1.equals(test2));
    }
}

And the resulting output from a test run:

Enter first phrase>CS AT WATERLOO
Enter second phrase>COOL AS WET ART
Is not an anagram.
AACELOORSTTW
AACELOORSTTW
false

Any and all help is greatly appreciated.


Use the Arrays.equals() method to compare two arrays. It will compare the elements of the arrays, whereas the default Object.equals() method will not.

Returns true if the two specified arrays of chars are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.


The .equals method of the array itself doesn't compare the contents of the array.

If you want to do that, you'll have to do it yourself - something like:

for(int i = 0; i < test1.length; i++) {
    if(test1[i] != test2[i]) {
        return false;
    }
}
return true;

EDIT: Or use the static Arrays.equals.

0

精彩评论

暂无评论...
验证码 换一张
取 消