Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果不在数组中,则我的“向数组添加单词”功能不起作用。我做错了什么?_Java_Arrays_Search_Boolean_Lookup - Fatal编程技术网

Java 如果不在数组中,则我的“向数组添加单词”功能不起作用。我做错了什么?

Java 如果不在数组中,则我的“向数组添加单词”功能不起作用。我做错了什么?,java,arrays,search,boolean,lookup,Java,Arrays,Search,Boolean,Lookup,我有一个数组,正在从文本文件中读取一系列单词。我试图做的是,如果扫描仪所在的当前单词不在数组中,则将其添加到数组中。如果该单词已经在数组中,则转到下一个单词并再次启动该过程-检查其是否在数组中,如果没有,则添加它,依此类推。问题是我加载了一个单词abc,但它不在数组中,所以我添加了它。然后我加载另一个单词x,它不在数组中,所以我添加了它。然后我再次尝试加载abc,它已经在数组中了,但还是被添加了。我的代码中需要更改什么 try { fileScanner = new Sc

我有一个数组,正在从文本文件中读取一系列单词。我试图做的是,如果扫描仪所在的当前单词不在数组中,则将其添加到数组中。如果该单词已经在数组中,则转到下一个单词并再次启动该过程-检查其是否在数组中,如果没有,则添加它,依此类推。问题是我加载了一个单词abc,但它不在数组中,所以我添加了它。然后我加载另一个单词x,它不在数组中,所以我添加了它。然后我再次尝试加载abc,它已经在数组中了,但还是被添加了。我的代码中需要更改什么

    try {
        fileScanner = new Scanner(inFile).useDelimiter("[ ,!?.0123456789]+");
        System.out.println("The input has been loaded successfully.");

        while (fileScanner.hasNext()) {
            currentWord = fileScanner.next().toUpperCase();

            // If the word is not found, add it to the array.
            if (ht.findWord(currentWord, ht.array) == false) {
                ht.fillTable(currentWord, ht.asciiSum(currentWord), ht.array);
            } else {
            // if the word is found, move on to the next word.
                break;
            }

        }
        fileScanner.close();
    } // end try
    catch (Exception e) {
        System.out.println("The input file has not been successfully loaded.");
    }



public boolean findWord(String word, String[] table) {
    boolean found = false;
    for (int i = 0; i < table.length; i++) {
        if (table[i] == word) {
            found = true;
            //System.out.println("The word " + word + " was found at " + table[i]);
        } else {
            found = false;
        }
    }
    return found;
}
试试看{
fileScanner=newscanner(infle).useDelimiter(“[,!?.0123456789]+”);
System.out.println(“输入已成功加载”);
while(fileScanner.hasNext()){
currentWord=fileScanner.next().toUpperCase();
//如果找不到该单词,请将其添加到数组中。
if(ht.findWord(currentWord,ht.array)==false){
ht.fillTable(currentWord,ht.ascisum(currentWord),ht.array);
}否则{
//如果找到该单词,请转到下一个单词。
打破
}
}
fileScanner.close();
}//结束尝试
捕获(例外e){
System.out.println(“未成功加载输入文件”);
}
公共布尔findWord(字符串字,字符串[]表){
布尔值=false;
对于(int i=0;i
要比较两个字符串,必须使用
equals()
函数

公共布尔等于(对象与对象)

将此字符串与指定的对象进行比较。如果 并且仅当参数不为null并且是 表示与此对象相同的字符序列

改为

if (table[i].equals(word)) {
因此,
findWord(stringword,String[]table){
不仅返回
false

另一点

 if (ht.findWord(currentWord, ht.array) == false) {
平等

if (!ht.findWord(currentWord, ht.array)) {

您的代码需要在
findWord
方法中稍作更改:

public boolean findWord(String word, String[] table) {
    for(int i = 0; i < table.length; i++) {
        if (table[i].equals(word)) {
            //this line is necessary because otherwise your loop
            //will continue setting found to false if there are
            //any other words in the array
            return true;
        }
    }
    return false;
}
public boolean findWord(字符串字,字符串[]表){
对于(int i=0;i
比较两个字符串表[i]==word的错误方法。您必须使用equals()谢谢您提供了所有这些信息。这非常有帮助。
public boolean findWord(String word, String[] table) {
    for(int i = 0; i < table.length; i++) {
        if (table[i].equals(word)) {
            //this line is necessary because otherwise your loop
            //will continue setting found to false if there are
            //any other words in the array
            return true;
        }
    }
    return false;
}