File indexing program not working

253 Views Asked by At

I have to make a program that reads each word a file and makes an index of which lines the word occurs on in alphabetical order.

for example, if the file had:

a white white dog crowded around the valley

the output should be:

a around: 2 crowded: 2 dog: 1 the: 2 valley: 1 white: 1, 1


When my file contains:

one fish two fish blue fish green fish cow fish milk fish dog fish red fish can you find a little lamb can you find a white calf

THE OUTPUT IS WRONG!: (NOT IN ALPHA ORDER)

a: 3 4 calf: 4 find: 3 4 4 lamb: 3 little: 3 white: 4 you: 3 4 blue: 1 can: 3 cow: 2 dog: 2 green: 1 1 2 2 2 2 milk: 2 red: 2 two: 1 1 1 fish: 1 one: 1


Here is my code::

INDEXMAKER MASTER CLASS

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);
    }
}

WORD CLASS

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;
    }
}

CLIENT

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

any help would be appreciated, thanks.

1

There are 1 best solutions below

4
On

I can't find your definition of compareTo. It seems this would be the key part of your program?

Properly implement your compareTo and confirm it works properly by printing the results of comparisons using System.out.println

Doing your own comparison is "ok" in that it will work if you do it properly. The other thing you could do would be to implement Comparable and then you can get Java to sort a list of words for you.