Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.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_File_Search_Text - Fatal编程技术网

Java 搜索文本文件

Java 搜索文本文件,java,file,search,text,Java,File,Search,Text,我正在写一个方法来搜索列表形式的单词文本文件,搜索用户输入的单词,但如果找到一个字母,程序将返回一个肯定的结果。例如,如果我搜索“f”,它将返回字典中有一个单词“f”,而没有 public static void Option3Method(String dictionary) throws IOException { Scanner scan = new Scanner(new File("wordlist.txt")); String s; String words[

我正在写一个方法来搜索列表形式的单词文本文件,搜索用户输入的单词,但如果找到一个字母,程序将返回一个肯定的结果。例如,如果我搜索“f”,它将返回字典中有一个单词“f”,而没有

public static void Option3Method(String dictionary) throws IOException {
    Scanner scan = new Scanner(new File("wordlist.txt"));
    String s;
    String words[] = new String[500];
    String word = JOptionPane.showInputDialog("Enter a word to search for");
    while (scan.hasNextLine()) {
        s = scan.nextLine();
        int indexfound = s.indexOf(word);
        if (indexfound > -1) {
            JOptionPane.showMessageDialog(null, "Word was found");
        } else if (indexfound < -1) {
            JOptionPane.showMessageDialog(null, "Word was not found");
        }
    }
}
publicstaticvoid选项3method(字符串字典)抛出IOException{
扫描仪扫描=新扫描仪(新文件(“wordlist.txt”);
字符串s;
字符串字[]=新字符串[500];
String word=JOptionPane.showInputDialog(“输入要搜索的单词”);
while(scan.hasNextLine()){
s=scan.nextLine();
int indexfound=s.indexOf(单词);
如果(indexfound>-1){
showMessageDialog(null,“找到单词”);
}否则如果(indexfound<-1){
showMessageDialog(null,“找不到Word”);
}
}
}
使用如下方法代替
字符串#indexOf

boolean matched = s.matches("\\b" + word + "\\b");
这将确保在具有单词边界的行中找到用户输入的单词


顺便说一句,不清楚你为什么要声明一个包含500个元素的字符串数组,你没有在任何地方使用它。

你说字母,你说单词,你到底在搜索什么

如果要搜索单词,必须搜索单词边界内的单词:regex\b,如所示

您可以将
}else if(indexfound<-1){
替换为
}else{
,因为未找到时返回-1>-1,否则,<-1不会出现。

您的“else if”语句应为

if (indexfound>-1)
{ 
    JOptionPane.showMessageDialog(null, "Word was found");
}
 else if (indexfound<-1)
 {
    JOptionPane.showMessageDialog(null, "Word was not found");}
 }
} else if (indexfound == -1){

因为如果找不到子字符串,indexOf方法会精确返回-1。

我发现您的代码中有一些地方让人困惑

  • 它看起来像是每个字输出一次结果,而不是整个文件输出一次
  • 当你有一本字典时,你通常每行只有一个单词,所以它要么匹配,要么不匹配。看起来你(和大多数其他答案)试图在一个较长的字符串中找到这个单词,这可能不是你想要的。如果你搜索“ee”,你不想在“beer”中找到它,对吗
因此,假设字典每行只有一个有效单词,并且您不想在整个字典中的任何地方找到该单词,那么这个简单的代码就可以做到这一点

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File("wordlist.txt"));
        String word = "Me";
        try {
            while (scanner.hasNextLine()) {
                if (scanner.nextLine().equalsIgnoreCase(word)) {
                    System.out.println("word found");
                    // word is found, no need to search the rest of the dictionary
                    return;
                }
            }
            System.out.println("word not found");
        }
        finally {
            scanner.close();
        }
    }
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File("wordlist.txt"));
        String word = "Me";
        try {
            while (scanner.hasNextLine()) {
                if (scanner.nextLine().equalsIgnoreCase(word)) {
                    System.out.println("word found");
                    // word is found, no need to search the rest of the dictionary
                    return;
                }
            }
            System.out.println("word not found");
        }
        finally {
            scanner.close();
        }
    }
}