Java 比较两个词

Java 比较两个词,java,hashmap,anagram,Java,Hashmap,Anagram,我是论坛的新手(作为注册用户),所以我非常努力(我发誓!)不发布问题,寻找旧的答案,我检查了其他人在类似问题上的错误,但我无法纠正任何错误 我的代码,在这里,应该检查一个单词是否是另一个单词的字谜。我很确定我的生活很复杂,本来有更简单的方法,但是。。。我已经在这方面工作了一段时间,现在希望看到它的工作 知道为什么没有吗 我所看到的只是空字典,当这两个单词的字母数相同时,它们总是字谜(这意味着事实上我的字典没有做任何事情:“() 导入acm.program.ConsoleProgram; 导入ja

我是论坛的新手(作为注册用户),所以我非常努力(我发誓!)不发布问题,寻找旧的答案,我检查了其他人在类似问题上的错误,但我无法纠正任何错误

我的代码,在这里,应该检查一个单词是否是另一个单词的字谜。我很确定我的生活很复杂,本来有更简单的方法,但是。。。我已经在这方面工作了一段时间,现在希望看到它的工作

知道为什么没有吗

我所看到的只是空字典,当这两个单词的字母数相同时,它们总是字谜(这意味着事实上我的字典没有做任何事情:“()

导入acm.program.ConsoleProgram;
导入java.util.*;
公共类字谜扩展控制台程序{
字符串第一个字;
字符串第二个字;
公共布尔校验长度(字符串第一个字、字符串第二个字){
if(firstWord.length()==secondWord.length()){
println(“相同长度!”);
返回true;
}否则{
返回false;
}
}
公共布尔字谜(字符串第一个字,字符串第二个字){
firstWord=firstWord.toLowerCase();
secondWord=secondWord.toLowerCase();
字符串[]firstArray=firstWord.split(\\a”);
字符串[]secondArray=secondWord.split(\\a”);
int firstLength=firstWord.length();
int secondLength=secondWord.length();
Map firstDictionary=newhashmap();
Map secondDictionary=newhashmap();
for(firstLength=0;firstLength==firstArray.length;firstLength++){
System.out.println(“检查字母”+firstArray[firstLength]+“in-array”+firstArray.toString());
if(firstDictionary.get(firstArray[firstLength])==null){
firstDictionary.put(firstArray[firstLength],1);
}否则{
firstDictionary.put(firstArray[firstLength],firstDictionary.get(firstArray[firstLength])+1);
}
}
for(secondLength=0;secondLength==secondArray.length;secondLength++){
if(secondDictionary.get(secondArray[secondLength])==null){
secondDictionary.put(secondArray[secondLength],1);
}否则{
secondDictionary.put(secondArray[secondLength],secondDictionary.get(secondArray[secondLength])+1);
}
}
if(第一字典等于(第二字典)){
返回true;
}否则{
返回false;
}
}
公开募捐{
int=0;
while(再次运行==0){
println(“输入要分析的第一个单词”);
firstWord=readLine();
println(“输入要分析的第二个单词”);
secondWord=readLine();
if(校验长度(第一个字,第二个字)==true){
if(字谜(第一个字,第二个字)==true){
println(“是的!这两个词是拼字法!”);
}
}否则{
println(“不,这两个词不是字谜!”);
}
}
}
}

循环条件中存在错误

  for (firstLength=0;firstLength==firstArray.length;firstLength++)
这应该是

   for (firstLength=0;firstLength<firstArray.length;firstLength++)
是错误的,您只需要char数组,因此改用此数组:

 char[] first = firstWord.toCharArray();
 char[] second = secondWord.toCharArray();
工作版本:

 public boolean anagram(String firstWord, String secondWord) {
        firstWord = firstWord.toLowerCase();
        secondWord = secondWord.toLowerCase();
        char[] firstArray = firstWord.toCharArray();
        char[] secondArray = secondWord.toCharArray();
        int firstLength = firstWord.length();
        int secondLength = secondWord.length();

        Map<Character, Integer> firstDictionary = new HashMap<>();
        Map<Character, Integer> secondDictionary = new HashMap<>();

        for (firstLength = 0; firstLength < firstArray.length; firstLength++) {
           // System.out.println("checking the letter " + firstArray[firstLength] + " in array" + firstArray.toString());
            if (!firstDictionary.containsKey(firstArray[firstLength])) {
                firstDictionary.put(firstArray[firstLength], 1);
            } else {
                firstDictionary.put(firstArray[firstLength], firstDictionary.get(firstArray[firstLength]) + 1);
            }
        }

        for (secondLength = 0; secondLength < secondArray.length; secondLength++) {
            if (!secondDictionary.containsKey(secondArray[secondLength])) {
                secondDictionary.put(secondArray[secondLength], 1);
            } else {
                secondDictionary.put(secondArray[secondLength], secondDictionary.get(secondArray[secondLength]) + 1);
            }
        }

        if (firstDictionary.equals(secondDictionary)) {
            return true;
        } else {
            return false;
        }
    }
public boolean anagram(字符串第一个字,字符串第二个字){
firstWord=firstWord.toLowerCase();
secondWord=secondWord.toLowerCase();
char[]firstArray=firstWord.toCharArray();
char[]secondArray=secondWord.toCharArray();
int firstLength=firstWord.length();
int secondLength=secondWord.length();
Map firstDictionary=newhashmap();
Map secondDictionary=newhashmap();
for(firstLength=0;firstLength
我会采用不同的方法:

String firstWord = "abcde";
String secondWord = "edcba";
String[] arr1 = firstWord.split("(?!^)"); // split each string to an array of (String) characters
Arrays.sort(arr1); // and sort it alphabetically 
String[] arr2 = secondWord.split("(?!^)");
Arrays.sort(arr2);
// now we can compare:
System.out.println(Arrays.equals(arr1, arr2)); // prints 'true'

KickButtowski是的,至少通过一些简单的测试,这实际上是一种有趣的解决方法。我没有想到这一点!
 public boolean anagram(String firstWord, String secondWord) {
        firstWord = firstWord.toLowerCase();
        secondWord = secondWord.toLowerCase();
        char[] firstArray = firstWord.toCharArray();
        char[] secondArray = secondWord.toCharArray();
        int firstLength = firstWord.length();
        int secondLength = secondWord.length();

        Map<Character, Integer> firstDictionary = new HashMap<>();
        Map<Character, Integer> secondDictionary = new HashMap<>();

        for (firstLength = 0; firstLength < firstArray.length; firstLength++) {
           // System.out.println("checking the letter " + firstArray[firstLength] + " in array" + firstArray.toString());
            if (!firstDictionary.containsKey(firstArray[firstLength])) {
                firstDictionary.put(firstArray[firstLength], 1);
            } else {
                firstDictionary.put(firstArray[firstLength], firstDictionary.get(firstArray[firstLength]) + 1);
            }
        }

        for (secondLength = 0; secondLength < secondArray.length; secondLength++) {
            if (!secondDictionary.containsKey(secondArray[secondLength])) {
                secondDictionary.put(secondArray[secondLength], 1);
            } else {
                secondDictionary.put(secondArray[secondLength], secondDictionary.get(secondArray[secondLength]) + 1);
            }
        }

        if (firstDictionary.equals(secondDictionary)) {
            return true;
        } else {
            return false;
        }
    }
String firstWord = "abcde";
String secondWord = "edcba";
String[] arr1 = firstWord.split("(?!^)"); // split each string to an array of (String) characters
Arrays.sort(arr1); // and sort it alphabetically 
String[] arr2 = secondWord.split("(?!^)");
Arrays.sort(arr2);
// now we can compare:
System.out.println(Arrays.equals(arr1, arr2)); // prints 'true'