Java 询问一个单词并检查其在文本文件中的出现次数 公共类尝试{ 公共静态void main(字符串args[])引发IOException{ BufferedReader in=新的BufferedReader(新文件读取器(“Try.txt”); 扫描仪sc=新的扫描仪(System.in); System.out.print(“输入要查找的子字符串:”); 字符串字=sc.next(); String line=in.readLine(); 整数计数=0; 字符串s[]; 做 { s=行分割(“”); 对于(int i=0;i

Java 询问一个单词并检查其在文本文件中的出现次数 公共类尝试{ 公共静态void main(字符串args[])引发IOException{ BufferedReader in=新的BufferedReader(新文件读取器(“Try.txt”); 扫描仪sc=新的扫描仪(System.in); System.out.print(“输入要查找的子字符串:”); 字符串字=sc.next(); String line=in.readLine(); 整数计数=0; 字符串s[]; 做 { s=行分割(“”); 对于(int i=0;i,java,text-files,Java,Text Files,我的程序的目的是要求用户输入一个或多个将在文本文件中检查的单词,如果它存在,它将计算用户输入的单词在文本文件中出现的次数。到目前为止,我的程序只能搜索一个单词。如果我尝试用空格分隔的两个单词,则只搜索第一个单词并计算其出现次数。有关于如何搜索多个单词的提示吗 我是按照问题的标题来做的,因此我建议使用以下算法: public class Try{ public static void main (String args[]) throws IOException { BufferedRea

我的程序的目的是要求用户输入一个或多个将在文本文件中检查的单词,如果它存在,它将计算用户输入的单词在文本文件中出现的次数。到目前为止,我的程序只能搜索一个单词。如果我尝试用空格分隔的两个单词,则只搜索第一个单词并计算其出现次数。有关于如何搜索多个单词的提示吗

我是按照问题的标题来做的,因此我建议使用以下算法:

public class Try{
public static void main (String args[]) throws IOException {
    BufferedReader in = new BufferedReader(new FileReader("Try.txt"));
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the subtring to look for: ");
    String Word=sc.next();
    String line=in.readLine();
    int count =0;
    String s[];
     do
     {
        s=line.split(" ");
        for(int i=0; i < s.length; i++) 
        {
            String a = s[i];
            if(a.contains(Word))
                count++;
        }
        line=in.readLine();
    }while(line!=null);
    System.out.print("There are " +count+ " occurences of " +Word+ " in ");
    java.io.File file = new java.io.File("Try.txt");
    Scanner input = new Scanner(file);
    while(input.hasNext())
    {
            String word = input.nextLine();
            System.out.print(word);
    }

  }
}

编辑: 如果要检查文档中的多个单词,请使用此选项

public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new FileReader("Test.txt"));
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the subtring to look for: ");
    String word = sc.next();
    String line = in.readLine();
    int count = 0;
    // here is where the efficiently magic happens
    do {
    // 1. you dont need to split a line by spaces, too much overhead...
    // 2. and you dont need to do counter++
    // 3. do instead: calculate the number of coincidences that the word is
    //repeated in a whole line...that is what the line below does..
        count += (line.length() - line.replace(word, "").length()) / word.length();
    //the rest looks fine

    //NOTE: if you need a whole word then wrap the input of the user and add the empty spaces at begin and at the end...so the match will be perfect to a word

        line = in.readLine();
    } while (line != null);
    System.out.print("There are " + count + " occurences of " + word + " in ");
    }

程序中的什么逻辑会让你认为它会搜索多个单词?您只有一个
if(a.contains(Word))
行和一个计数器。您需要为每个输入的单词保留计数器。
String word=sc.next()
这意味着您正在将一个单词读入名为
word
字符串中。即使你要修改这行,你也需要做更多的工作来确保你要查找的两个单词不相邻。您还可以运行调试器来检查
Word
的内容,查看它是否只包含一个值。看,这要短得多,但问题似乎仍然存在。我尝试输入两个单词,但只记录了第一个单词的出现(使用您的算法)。有没有办法“解决”这个问题?顺便说一句,谢谢你的建议:)我肯定会注意到这些。@REA98,我添加了一个编辑部分。。。这段代码应该满足您的要求。。请给我一个尝试,让我知道…我只添加了一些调整,它的工作。非常感谢:)我还有最后一个问题,在普通for循环中,增强的循环等效于什么?我从来没有使用过增强循环(这让我有点困惑),所以我对这一部分确实有一点想法。嗯,将孔循环替换为(int I=0;Ipublic static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader("Test.txt")); Scanner sc = new Scanner(System.in); System.out.print("Enter the subtring to look for: "); String input = sc.nextLine(); String[] word = input.split(" "); String line = in.readLine(); int count = 0; do { for (String string : word) { count += (line.length() - line.replace(string, "").length()) / string.length(); } line = in.readLine(); } while (line != null); System.out.print("There are " + count + " occurences of " + Arrays.toString(word) + " in "); }