Traversing ArrayList of superclass, help? :[
Hello. This has been driving me crazy for a few days now.
I have a homework assignment that involves making an index creating program. Basically, the program reads a text file and outputs another text file as an "index" of all the words and their line numbers from the input file.
One class is called "IndexEntry". Every instance of this class represents one line on the output file, essentially holding a String of the word and its line numbers. Here's its code:
Code :
import java.util.*;
public class IndexEntry
{
private String word;
private ArrayList<Integer> numsList;
public IndexEntry(String aWord)
{
word = aWord.toUpperCase();
numsList = new ArrayList<Integer>();
}
public void add(int num)
{
/*Integer intObj = new Integer(num);
if (numsList.contains(intObj) == false)
numsList.add(intObj);*/
numsList.add(num);
}
public String getWord()
{
return word;
}
public String toString()
{
String tempString = word + " ";
for (Integer i : numsList)
tempString += i + ", ";
return tempString;
}
}
I commented some stuff out in the add(int num) method because it seems that an ArrayList can in fact take ints before turning them into objects and storing them.
Next is the "DocumentIndex" class. It extends an ArrayList of IndexEntry and represents all of the IndexEntry instances (I think). Here is it's code:
Code :
import java.util.*;
public class DocumentIndex extends ArrayList<IndexEntry>
{
public DocumentIndex()
{
super();
}
public DocumentIndex(int initialCapacity)
{
super(initialCapacity);
}
public void addWord(String word, int num)
{
}
public void addAllWords(String str, int num)
{
}
private int foundOrInserted(String word)
{
}
}
As you can see, there isn't much there yet.
There's also a third class with the main method and some other important stuff, but I don't think it pertains to my problem and so I won't post it unless requested.
Here are my problems:
1) I don't really understand how "DocumentIndex" can extend an ArrayList of "IndexEntry"s. What does this mean? What is inherited?
2) "DocumentIndex" needs to look at every instance of "IndexEntry" to see if a word already exists in an entry before inserting it into a new IndexEntry (which is what "DocumentIndex"'s add method is for). How do I do this? I thought about using a for-each loop, something we just learned in my programming class, but no luck.
I don't think my problem is too complicated but I'm a total novice and was just introduced to ArrayLists and Arrays last week. I've done some Google searching and should've gone to bed awhile ago but I can't find any answers. If you can help me, thank you.
Re: Traversing ArrayList of superclass, help? :[
Actually, here's the third class with the main. I know double-posting is frowned upon but I didn't want to overencumber the first post. So should anyone actually care to try to run this thing, here's the "IndexMaker" class, untouched (i.e. this was pre-made for the assignment):
Code :
// This program takes a text file, creates an index (by line numbers)
// for all the words in the file and writes the index
// into the output file. The program takes input and output file names
// from the command-line args or prompts the user for the file names.
import java.util.Scanner;
import java.io.*;
public class IndexMaker
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
String fileName;
// Open input file:
if (args.length > 0)
fileName = args[0];
else
{
System.out.print("\nEnter input file name: ");
fileName = keyboard.nextLine().trim();
}
BufferedReader inputFile =
new BufferedReader(new FileReader(fileName), 1024);
// Create output file:
if (args.length > 1)
fileName = args[1];
else
{
System.out.print("\nEnter output file name: ");
fileName = keyboard.nextLine().trim();
}
PrintWriter outputFile =
new PrintWriter(new FileWriter(fileName));
// Create index:
DocumentIndex index = new DocumentIndex();
String line;
int lineNum = 0;
while ((line = inputFile.readLine()) != null)
{
lineNum++;
index.addAllWords(line, lineNum);
}
// Save index:
for (IndexEntry entry : index)
outputFile.println(entry);
// Finish:
inputFile.close();
outputFile.close();
System.out.println("Done.");
}
}
I'm going to bed now, I hope someone has an answer by the time I get off of work tomorrow. Blegh.