Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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中使用try/catch块时,计算特定单词在txt文件中出现的次数_Java_Oop_Exception Handling - Fatal编程技术网

在java中使用try/catch块时,计算特定单词在txt文件中出现的次数

在java中使用try/catch块时,计算特定单词在txt文件中出现的次数,java,oop,exception-handling,Java,Oop,Exception Handling,我的目标是让程序在忽略诸如“secretive”和“secsecsecret”等词的情况下,根据文本文档中出现“secret”一词的次数来运行计数。我能够让它计数,但它只返回单词出现的2次,而不是单词出现在文档中的4次,并且包含我添加的“secsecsecret”。是否可以添加某种例外声明来防止它在文档中拾取“secsecret”?我不确定我做了什么错误的事情,因为它只发现了两次“秘密”这个词。这是我的面向对象编程课的一个实验作业 以下是我到目前为止的情况: package lab4; imp

我的目标是让程序在忽略诸如“secretive”和“secsecsecret”等词的情况下,根据文本文档中出现“secret”一词的次数来运行计数。我能够让它计数,但它只返回单词出现的2次,而不是单词出现在文档中的4次,并且包含我添加的“secsecsecret”。是否可以添加某种例外声明来防止它在文档中拾取“secsecret”?我不确定我做了什么错误的事情,因为它只发现了两次“秘密”这个词。这是我的面向对象编程课的一个实验作业

以下是我到目前为止的情况:

package lab4;

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



public class Lab4 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception{

        String keyword = "secret";
        int c = 0;

        try
        {
            Scanner file = new Scanner(new File("/Users/Taba/Desktop/Lab4textdoc.txt"));
            while (file.hasNext())
            {
                 keyword = file.nextLine();
                if (! file.hasNextLine())
                {
                    System.out.println("Invalid file format");
                    String invalidData = file.nextLine();
                }
                else
                {
                    keyword = file.nextLine();
                    String newLine = file.nextLine();
                    c++;
                    System.out.println("The keyword " + keyword + " appears " + c + " times." );
                }
            }
                file.close();
        }

            catch(FileNotFoundException fnfe)
                {
                System.out.println("Unable to find Lab4textdoc.txt, exiting");
                }
            catch(NoSuchElementException nsee)
                {
                System.out.println("Attempt to read past the end of the file");
                }

    }

}
以下是我在文本文档中的内容:

secret
secsecret
secretive
secret
Secret
secret
这就是它给我的输出:

run:
The keyword secsecret appears 1 times.
The keyword Secret appears 2 times.
BUILD SUCCESSFUL (total time: 0 seconds)

您遇到的第一个问题是,您正在覆盖您希望在此处找到的单词:

keyword = file.nextLine();
您需要声明另一个string对象,并将其与关键字进行比较

另一方面,您没有指定单词是否必须匹配敏感度,因为在java中字符串“Secret”与“Secret”或“Secret”不同

对于这两个选项,基本上需要等于或

  • 每次调用
    nextLine
    ,都会读取文件的下一行。因此,每次迭代只需调用一次(仅在
    while
    循环中调用)。当你在循环中不止一次地呼叫时,你会跳过很多行而不去看它们
  • 你不是在数你想要什么。如果(是我想要的字符串)c++,则应该包含
    。但是你在不测试任何东西的情况下增加了c,导致了错误的结果
您的代码应该类似于:

while (file.hasNext()) {
    keyword = file.nextLine();
    if ("secret".equals(keyword)) {
        c++;
    }
}
System.out.println("The keyword " + keyword + " appears " + c + " times." );

如果你想计算更多的单词(如“Secret”),你应该为每一个单词创建一个不同的计数器,并为每种情况分别执行一个
If

在处理前你要读一行两次,因此实际上每隔一行跳过一行
String newLine=file.nextLine()
您每隔一行“忽略”一次-为什么?而且您的变量
c
不计算单词的出现次数-每次点击
else
分支时,它都会增加。感谢您的帮助并提醒我注意这些错误。该程序现在的工作方式应该是这样的。我想我可能已经看了太久,没有意识到我的错误是什么。正如我的指导老师经常说的,如果您无法找出哪里出了问题,让其他人检查您的代码总是有帮助的。再次感谢!只是想知道-
c++
Java
中编译吗?