【问题标题】:Counting words from a file计算文件中的单词
【发布时间】:2015-11-25 22:23:50
【问题描述】:

对于我必须编写的这个程序,我得到了一个包含一行(或多行)文本的字符串输入文件,例如:“High HIGH cat High jumped (WOW 6SOFT)”。从这一行开始,我必须扫描文件,计算单词出现的次数(无论大小写),然后将其输出到格式化文件。如果数字出现在单词之前,则不应计算该单词。格式必须以三个空格右对齐的计数开头,然后是另一个空格,然后是小写的单词。

package InClass;

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


public class CountWords {
public static void main(String[] args) {
    try {
        File inputFile = new File("input.txt");
        Scanner scanner = new Scanner(inputFile);
        File outputFile = new File("output.txt");
        PrintWriter writer = new PrintWriter(outputFile);

        boolean next = scanner.hasNext();
        ArrayList<String> inputWords = new ArrayList<String>();
        ArrayList<String> outputWords = new ArrayList<String>();

        while (next) { // Adds all the strings to an array list
            inputWords.add(scanner.next());
            for (int i = 0; i < inputWords.size(); i++) {
                inputWords.get(i).toLowerCase();
            }
        }
        while (next) {
            for (int i = 0; i < inputWords.size(); i++) {
                String word = inputWords.get(i);
                int count = 1;
                if (!Character.isDigit(word.charAt(0))) {
                    outputWords.add(inputWords.get(i));
                    if (outputWords.contains(word)) {
                        count++;
                    }
                } else {
                    inputWords.remove((i));
                }
                for (int j = 0; i < outputWords.size(); j++) {
                    word = outputWords.get(i);
                    writer.printf("%3s" + "%1s\n", count, word);
                }
            }
        }
        scanner.close();
        writer.close();
    } catch (FileNotFoundException e) {
    }
}
}

我的测试器文件给我一个错误,说只有一个测试器类存在 NoSuchElementException,它正在测试两个单词。这是给出错误的测试类”

@Test
public void testTwoWords() {
    try {
        File inputFile = new File(INPUT);
        File outputFile = new File(OUTPUT);

        // If assert fails it is (usually) because the file was (wrongly)
        // left open in an earlier run.
        // Using a file manager application (e.g. explorer), go to project
        // directory and delete it.
        // Make sure that your program closes these files before ending.
        if (inputFile.exists()) {
            assertTrue("Your program left \"" + INPUT
                    + "\" open in a previous test.", inputFile.delete());
        }
        if (outputFile.exists()) {
            assertTrue("Your program left \"" + OUTPUT
                    + "\" open in a previous test.", outputFile.delete());
        }

        // create INPUT file
        PrintWriter input = new PrintWriter(inputFile);
        input.println("King");
        input.println("");
        input.println("");
        input.println("");
        input.println("hill");
        input.close();

        // invoke program
        CountWords.main(null);

        // verify OUTPUT file exists and is empty
        assertTrue("Output file doesn't exist", outputFile.exists());
        Scanner output = new Scanner(outputFile);
        String actual = output.nextLine();
        assertEquals("Incorrect result", "  1 king", actual);
        actual = output.nextLine();
        assertEquals("Incorrect result", "  1 hill", actual);
        assertFalse("There should be no more data", output.hasNext());
        output.close();

        // delete I/O files
        assertTrue("Input file could not be deleted", inputFile.delete());
        assertTrue("Output file could not be deleted", outputFile.delete());
    } catch (IOException e) {
        fail("No exception should be thrown");
    }

}

最后,我的其他测试人员说“您的程序在之前的测试中打开了“input.txt””。知道为什么吗?提前感谢您!

【问题讨论】:

    标签: java io


    【解决方案1】:

    您的 outputWords 的 for 循环使用 i 循环计数器而不是 j。

    for (int j = 0; i < outputWords.size(); j++) {
                        word = outputWords.get(i);
                        writer.printf("%3s" + "%1s\n", count, word);
                    }
    

    改成

    for (int j = 0; i < outputWords.size(); j++) {
                        word = outputWords.get(j);//NEED TO USE J HERE
                        writer.printf("%3s" + "%1s\n", count, word);
                    }
    

    其他测试失败的原因是因为您显然应该在每次测试运行后删除输出文件。

    【讨论】:

    • 我不完全明白该怎么做,只是 inputFile.delete() 吗?我尝试将其添加到我的代码中,但没有成功
    猜你喜欢
    • 2011-05-04
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 2011-04-25
    相关资源
    最近更新 更多