java - 不会比较从 PrintWriter 读入的 2 个相等的字符串

标签 java hashtable string-comparison

我正在编写一个程序,该程序采用 PrinterWriter 的一个程序创建的文档,然后将该文档中的行散列到新程序中的数组中。哈希值是通过使用字母的 ASCII 代码并将它们相加来完成的。我能够获得每行的正确哈希值并将其保存在哈希表中。顺便说一句,它是经过哈希处理的国家/地区列表。我的问题是,它似乎无法将用户输入的国家/地区(即使是复制和粘贴)与哈希表中的国家/地区进行比较以显示它们。它不仅应该显示哈希表中的国家/地区,还应该显示指向哈希表的所有国家/地区。因此,如果应该去位置 23 但去了位置 26,则显示 23-26 以显示聚类。我已经厌倦了一切让它发挥作用,但似乎没有任何作用,请帮忙。我已经包含了一些代码:

import java.io.PrintWriter;
import java.io.File;
import java.util.*;
import java.text.*;

public class Hashing
{
    String[] line = new String[238];
    String[] HashTable = new String[300];

    public Hash() {
        for (int i = 0; i< HashTable.length; i++) {
            HashTable[i]=null;
        }
    }

    public void readIn()throws Exception {
        Scanner ln = new Scanner(new File(System.getProperty("user.home"),     "user.home.CountryUnSortedFormat.txt"));
        int i = 0;
        while (ln.hasNextLine()) {
            line[i] = ln.nextLine();
            i++;
        }
    }

    public int toASCII(String input) {
        int total = 0;
        char character;
        String str = input.replaceAll(",","").trim();
        if (str.length() > 50) {
            for (int i = 0; i<50; i++) {
                int ascii = str.charAt(i);
                if (ascii > 32) {
                    total = total + ascii;
                }
            }
        } else if (str.length()<50) {
            for (int i = 0; i<str.length(); i++) {
                int ascii = str.charAt(i);
                if (ascii > 32) {
                    total = total + ascii;
                }
            }
        }
        return total % 300;
    }

    public void hashIt(String input, int where){
        int counter = where;  
        if (where==299 && HashTable[where]!=null){
            counter = 0;
        }
        while (HashTable[counter]!=null){
            counter++;
        }
        System.out.println("Country = " + input + " HashValue = " + where + " actual HashSpot = " + counter);
        HashTable[counter]=input;
    }

public boolean showCountries(String paramCountry, int where){
int location = where;
int length = paramCountry.length();
while (!(HashTable[location].substring(0,length).contains(paramCountry))){
System.out.println("Input = " + paramCountry + " and HashTableCOunty = " +     HashTable[location].substring(0,length));
System.out.println("The Hash Table Index is " + location + " " + HashTable[location]);
     if (!(HashTable[location].substring(0,length).contains(paramCountry))){
     location++;
     }
     else if (HashTable[location].substring(0,length).contains(paramCountry)){
     System.out.println("The Hash Table Index is " + location + " " + HashTable[location]);
     System.out.println("Eguals");
     return true;
     }
     if (location==300||HashTable[location]==null){
     System.out.println("End");
     return false;
     }
     }
 return false;
 } 


    public void displayHashTable() {
        for (int i = 0; i<HashTable.length; i++) {
            System.out.println("i = " + i + " " + HashTable[i]);
        }
    }

    public static void main(String[]args)throws Exception {
        Scanner kb = new Scanner(System.in);

        Hash H = new Hash();
        H.readIn();
        for (int i = 0; i< 238; i++) {
            int where = H.toASCII(H.line[i]);
            H.hashIt(H.line[i], where);
        }
        H.displayHashTable();

        String UserChoice;
        System.out.println("Enter the Name of the Country you wish to locate in the Hash Table or Enter -1 to quit: ");
        UserChoice = kb.nextLine();
        while (!(UserChoice.equalsIgnoreCase("-1"))) {
            int index = H.toASCII(UserChoice);
            boolean error = H.showCountries(UserChoice, index);
            while (error == false) {
                System.out.println("The country you searched for is not in the hash table.  Try again.");
                UserChoice = kb.nextLine();
                index = H.toASCII(UserChoice);
                error = H.showCountries(UserChoice, index);
            }
            System.out.println("Enter the Name of the Country you wish to locate in the Hash Table or Enter -1 to quit: ");
            UserChoice = kb.nextLine();
        }
    }
}

最佳答案

让我们看看showCountries方法:

public boolean showCountries(String paramCountry, int where) {
    //....
    return false;
}

我删除了不包含 return 的每一行陈述。正如你所看到的,你总是返回 false无论是否找到所搜索的元素。

因此这个循环:

while (error == false) {
    //...
}

就像一个无限循环。

更改 showCountries 中的代码方法返回true ,这个国家被发现了。

并考虑更改变量名称error到别的东西。 error == false听起来好像“一切都好”,但这里的情况并非如此。

如果我正确理解您的代码,您可以更改此内容:

else if (paramCountry.equals(HashTable[location].substring(0,length))) {
    System.out.println("The Hash Table Index is " + location + " " + HashTable[location]);
    break;
}

至:

else if (paramCountry.equals(HashTable[location].substring(0,length))) {
    System.out.println("The Hash Table Index is " + location + " " + HashTable[location]);
    return true;
}

编辑:

另一个容易出错的点就在这里:

int length = paramCountry.length()-1;

while (!(paramCountry.equals(HashTable[location].substring(0,length)))) {
//...

由于使用 -1,您将截断最后一个字符。 一个小例子:

paramCountry = "Eng";
HashTable[0] = "England";
int length = paramCountry.length()-1; // 2 (paramCountry.length() is 3)

这是使用上述值的结果:

HashTable[0].substring(0,length)) // "En"
paramCountry.equals(HashTable[0].substring(0, length)) // "Eng".equals("En") -> false

所以,您可以删除 -1或者摆脱它substring并使用 contains 相反。

编辑2:

因此,编辑后使用 contains而不是substring你只剩下一个错误(我当前看到的最后一个错误):

while (!(HashTable[location].substring(0, length).contains(paramCountry))) {
    // ...
}
return false;

在调用方法之前 showCountries您通过调用 H.toASCII(UserChoice); 来计算可能的位置。该位置被指定为 location 的方法上面的while中使用了它环形。将跳过此循环,因为已经找到搜索国家/地区。不好的是:你会返回false在这种情况下。

现在我建议将此返回更改为 return true;因为只有当已找到搜索的国家/地区(并且跳过 while 循环)时才会到达此行。如果找不到该国家,则返回false在此 if 主体中: if (location==300||HashTable[location]==null) .

关于java - 不会比较从 PrintWriter 读入的 2 个相等的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27028631/

相关文章:

Java:比较整数和字符串 - 性能

java - 如何使用 lambda 表达式创建满足条件的数组列表

JavaScript - 比较字符串并从其中一个字符串中删除第一个字符,直到它们相等

c - 小整数 vector 的高效比较

java - 在 Nashorn 中区分 null 和 undefined 值

perl - 在 Perl 中,如何处理整个哈希?

java - HashMap 内部使用 Node<K, V> 数组,而 Hashtable 内部使用 Map.Entry<K, V> 数组,为什么会出现这种内部差异?

delphi - Delphi 5 的哈希表实现

c++ - 像 strcmp 那样比较两个 std::string

java - 插入查询适用于 phpMyAdmin,但不适用于 Java