Java 搜索文本文件中的特定单词-程序将单词片段计算为匹配项

Java 搜索文本文件中的特定单词-程序将单词片段计算为匹配项,java,Java,我的程序将把单词hand计算为匹配项,因为它包含单词and。我不知道如何修复它,尤其是因为搜索词被声明为用户输入 假设你的搜索信永远不会是每个句子的第一个单词。最简单的解决方法是在单词前添加一个额外的空格 类似于searchW=”“+searchW您的代码还有另一个问题-如果您在一行中有多个匹配项,它将只计算一次。这两种情况的原因是搜索文件的方式——检查每一行是否包含字符串。如果您想要单词的精确匹配,最简单的方法是这样标记行: If I wanna search for a word like

我的程序将把单词hand计算为匹配项,因为它包含单词and。我不知道如何修复它,尤其是因为搜索词被声明为用户输入

假设你的搜索信永远不会是每个句子的第一个单词。最简单的解决方法是在单词前添加一个额外的空格


类似于
searchW=”“+searchW

您的代码还有另一个问题-如果您在一行中有多个匹配项,它将只计算一次。这两种情况的原因是搜索文件的方式——检查每一行是否包含字符串。如果您想要单词的精确匹配,最简单的方法是这样标记行:

 If I wanna search for a word like "and" for example and my text file is 


Saturday and Sunday 
Hand 
现在您有了一个字符串数组,每个字符串都是当前正在处理的行中的一个单词。接下来,您可以使用
equalsIgnoreCase()
进行简单比较(奖金-即使单词大写也有效)

当然,如果需要的话,可以修改传递给split函数的delimeters,使其包含标点符号等

String[] tokens = text.split("\\s+");
建议: 1.不要使用file.exists()检查文件。它主要是检查目录。如果文件路径无效,它将给出异常。
2.使用do while从用户处获取有效路径。

Javascript问题标签已删除——此问题与Javascript无关。后面还有一个空格,但假设该单词不是最后一个(例如hand-handy)为什么要删除您的程序。这是对以前程序的修改。
public class Test
{
  public static void main(String[] args) throws IOException
  {
    Scanner scan = new Scanner(System.in);
   File file=null;
   boolean inputFile=false;
    do
    {
      System.out.print("Enter a filename:");
      String fileName = scan.nextLine();
       inputFile= new File(fileName).isFile();
       if(inputFile){
           file=new File(fileName);
           inputFile=true;
       }

    }while(!inputFile);

    Scanner scanned = new Scanner(System.in); 
    System.out.println("Enter a word to search for: ");
    String searchW = scanned.next();
    int count = 0;

    Scanner in = new Scanner(file);
    String text=null;
    while(in.hasNextLine())
    { 
        text=in.nextLine();
      if(text.contains(searchW))
      { 
        count++;
        System.out.println(text); 
      }
    }
    System.out.println("The word" + " " + searchW + " " +  "was found" + " " + count + " " + "time(s) in this file");
    scan.close();
    in.close();
    scanned.close();
   }
  }
public static void main(String[] args) throws IOException {
                Scanner scan = new Scanner(System.in);
                System.out.println("Please enter the filename: ");
                String filename = scan.nextLine();
                System.out.println("Please enter a word: ");
                String wordname = scan.nextLine();

                int count = 0;
                try (LineNumberReader r = new LineNumberReader(new FileReader(filename))) {
                    String line;
                    while ((line = r.readLine()) != null) {
                        for (String element : line.split(" ")) {
                            if (element.equalsIgnoreCase(wordname)) {
                                count++;
                                System.out.println("Word found at line " + r.getLineNumber());
                            }
                        }
                    }
                }
                System.out.println("The word " + wordname + " appears " + count + " times.");
            }