【问题标题】:File indexing program not working文件索引程序不工作
【发布时间】:2014-01-08 18:21:32
【问题描述】:

我必须编写一个程序,将每个单词读取为一个文件,并按字母顺序对单词出现在哪些行进行索引。

例如,如果文件有:

一只白白的狗 山谷里挤满了人

输出应该是:

一个 周围:2 拥挤:2 狗:1 : 2 山谷:1 白色:1、1


当我的文件包含:

一条鱼两条鱼蓝鱼绿鱼 牛鱼 牛奶鱼 狗鱼 红鱼 你能找到一只小羊吗 你能找到一头白色的小牛吗

输出错误!:(不是 ALPHA 顺序)

一个:3 4 小牛:4 找到:3 4 4 羊肉:3 小:3 白色:4 你:3 4 蓝色:1 可以:3 牛:2 狗:2 绿色:1 1 2 2 2 2 牛奶:2 红色:2 二:1 1 1 鱼:1 一:1


这是我的代码::

索引大师班

import java.io.*;
import java.util.*;

public class IndexMaker {
    private ArrayList<Word> words;
    private String fileName;
    private String writeFileName;

    public IndexMaker(String fileName, String writeFileName) {
        this.fileName = fileName;
        this.writeFileName = writeFileName;
        words = new ArrayList<Word>();
    }

    public void makeIndex() {
        try {
            File file = new File(fileName);
            Scanner lineScanner = new Scanner(file);
            int lineNum = 0;
            while (lineScanner.hasNext()) {
                lineNum++;
                Scanner wordScanner = new Scanner(lineScanner.nextLine());
                while (wordScanner.hasNext()) {
                    String word = wordScanner.next().toLowerCase();
                    if (!words.contains(new Word(word))) {
                        insertInto(word, findPosition(word), lineNum);
                    } else {
                        addLineNum(word, lineNum);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void displayIndex() {
        try {
            //FileWriter fileWriter = new FileWriter(new File(writeFileName));
            //BufferedWriter writer = new BufferedWriter(fileWriter);
            for (Word word : words)
                System.out.println(word.getWord() + ": " + word.getLineNums());
        } catch (Exception e) {
        }
    }

    private int findPosition(String word) {
        for (int i = 0; i < words.size(); i++) {
            if (word.compareTo(words.get(i).getWord()) <= 0)
                return i;
        }
        return 0;
    }

    private void insertInto(String word, int pos, int lineNum) {
        words.add(pos, new Word(word, String.valueOf(lineNum)));
    }

    private void addLineNum(String word, int lineNum) {
        int pos = findPosition(word);
        words.get(pos).addLineNum(lineNum);
    }
}

词类

public class Word {
    private String word;
    private String lineNums;
    public Word(String word, String lineNum) {
        this.word = word;
        this.lineNums = lineNum;
    }
    public Word(String word) {
        this.word = word;
        this.lineNums = "";
    }
    public String getWord() {
        return word;
    }
    public String getLineNums() {
        return lineNums;
    }
    public void addLineNum(int num) {
        lineNums += " " + num;
    }
    @Override
    public boolean equals(Object w) {
        if (((Word)w).getWord().equals(word))
            return true;
        else
            return false;
    }
}

客户

public class Client {
    public static void main(String[] args) {
        IndexMaker indexMaker = new IndexMaker("readme.txt", "readme.txt");
        indexMaker.makeIndex();
        indexMaker.displayIndex();
    }
}

任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: java indexing bluej alphabetical


    【解决方案1】:

    我找不到您对 compareTo 的定义。看来这将是您计划的关键部分?

    正确实现您的 compareTo 并通过使用 System.out.println 打印比较结果来确认它正常工作

    进行自己的比较是“可以的”,因为如果您正确地进行比较,它将起作用。您可以做的另一件事是实现 Comparable,然后您可以让 Java 为您排序单词列表。

    【讨论】:

    • compareTo 只是比较不需要的字符串
    • 也许我完全误解了你的问题,但看起来你说的问题是输出不是 alpha 顺序。
    • 这是主要问题
    • 那我误会了什么?您的主要问题是单词乱序,但您不想向我们展示您是如何设计确定正确顺序的方法的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 2014-10-17
    • 1970-01-01
    相关资源
    最近更新 更多