Java 我得到了一个“答案”;错误:无法取消对int的引用;我的程序设计为顺序、迭代和递归搜索.txt文件

Java 我得到了一个“答案”;错误:无法取消对int的引用;我的程序设计为顺序、迭代和递归搜索.txt文件,java,Java,在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 n

在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);
}
import java.io.*;
导入java.util.Scanner;
导入java.util.ArrayList;
公共类搜索扩展了对象{
公共静态最终字符串至_STOP=“-1”;
公共静态final int NOT_FOUND=-1;
公共静态整数计数1;
公共静态整数计数2;
公共静态整数计数3;
公开检索()
{
count1=0;
count2=0;
count3=0;
}
公共静态int序列搜索(ArrayList数组,字符串值)
{
int低=0;
int high=array.size()-1;
对于(int i=low;i您不能在int上使用compareTo()或等于,因为int是基元类型。您必须使用“==”来比较基元。
只能对Integer对象调用compareTo()或equals()方法

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


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

无需扩展对象类。它是自动扩展的。另外,请检查您的if语句。它充满了(if something 1)…else if(something 1)。这肯定是不正确的。最初,我用==代替了.compareTo().然而,它给了我在OP中给出的输出,尽管这个词在我的.txt文件中。