0

在 main 方法中,我尝试了 .equals() 和 .compareTo(),它们在第 88、94 和 100 行都给了我同样的错误。当我使用 == 时,它会编译,但在它运行:

Type the word you're searching for. Or type -1 to stop: curse
sequentialSearch()      : curse is not found (comparison=13040).
iterative binarySearch(): curse is not found (comparison=13).
recursive binarySearch(): curse is not found (comparison=13).
Type the word you're searching for. Or type -1 to stop:

我知道“诅咒”一词在我正在搜索的文本文件中。

这是我到目前为止的代码。

import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;

public class Search extends Object {
public static final String TO_STOP = "-1";
public static final int NOT_FOUND = -1;

public static int count1;
public static int count2;
public static int count3;

public Search()
{
    count1 = 0;
    count2 = 0;
    count3 = 0;
}

public static int sequentialSearch(ArrayList<String> array, String value)
{
    int low = 0;
    int high = array.size() - 1;
    for (int i = low; i <= high; i++){
        count1++;
        if (array.get(i) == value)
            return i;
    }
    return NOT_FOUND;
}

public static int binarySearch(ArrayList<String> array, String value)
{
    int low = 0;
            int high = array.size() - 1;
            while (low <= high) {
                int mid = (low + high)/2;
                if (array.get(mid) != value){
                    count2++;
                    high = mid - 1;
                } else if (array.get(mid) != value){
                    count2++;
                    low = mid + 1;
                } else
                    return mid;
            }
    return NOT_FOUND;
}

public static int binarySearch(ArrayList<String> array, int low, int high, String value)
{
    if (low > high)
                return NOT_FOUND;
            int mid = (low + high)/2;
            if (array.get(mid) != value){
                count3++;
                return binarySearch(array, low, mid-1, value);
            } else if (array.get(mid) != value){
                count3++;
                return binarySearch(array, mid+1, high, value);
            } else
        return mid;
}

public static void main (String [] args) throws IOException
{
    ArrayList<String> array = new ArrayList<String>();
    File fn = new File("sortedWords.txt");
    Scanner sc = new Scanner(fn);
    Scanner keyboard = new Scanner(System.in);
    boolean wantsToContinue = true;

        while(sc.hasNextLine()){
        String ln = sc.nextLine();
        array.add(ln);
        }

        do {
            System.out.print("Type the word you're searching for. Or type " + TO_STOP + " to stop: ");
            String word2search = keyboard.nextLine();

            if(word2search.equals(TO_STOP)){
                wantsToContinue = false;
            }
            else {
                int index;
                index = sequentialSearch(array, word2search);
                if (index.compareTo(NOT_FOUND))
                    System.out.println("sequentialSearch()      : " + word2search + " is not found (comparison=" + count1 + ").");
                else
                    System.out.println("sequentialSearch()      : " + word2search + " is found in [" + index + "] (comparison=" + count1 + ").");

                index = binarySearch(array, word2search);
                if (index.compareTo(NOT_FOUND))
                    System.out.println("iterative binarySearch(): " + word2search + " is not found (comparison=" + count2 + ").");
                else
                    System.out.println("iterative binarySearch(): " + word2search + " is found in [" + index + "] (comparison=" + count2 + ").");

                index = binarySearch(array, 0, array.size()-1, word2search);
                if (index.compareTo(NOT_FOUND))
                    System.out.println("recursive binarySearch(): " + word2search + " is not found (comparison=" + count3 + ").");
                else
                    System.out.println("recursive binarySearch(): " + word2search + " is found in [" + index + "] (comparison=" + count3 + ").");
                        }
    } while (wantsToContinue);
}

}

4

1 回答 1

1

您不能在 int 上使用 compareTo() 或 equals,因为 int 是原始类型。您必须使用 '==' 来比较原始类型。您只能在 Integer 对象上调用 compareTo() 或 equals() 方法。

第一个问题是您正在逐行读取输入文件并将每一行作为数组的条目。然后您尝试将输入的单词与文件中的整行进行比较。如果您正在尝试进行单词搜索,请尝试拆分将文件内容输入单词。

另一个错误是使用 == 来比较字符串。您不能使用 == 来比较字符串。请改用 equals() 方法。

于 2013-02-18T06:58:02.120 回答