Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Traversing ArrayList of superclass, help? :[

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

     
    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:

    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.


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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):

     
    //  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.

Similar Threads

  1. Overriding Superclass Static Methods, Using the Superclass Method
    By snowguy13 in forum Java Theory & Questions
    Replies: 10
    Last Post: December 21st, 2011, 08:30 AM
  2. Using an array which is in a superclass
    By razorfever in forum Object Oriented Programming
    Replies: 2
    Last Post: October 29th, 2011, 01:05 PM
  3. [SOLVED] Traversing a tree
    By IAmHere in forum Algorithms & Recursion
    Replies: 3
    Last Post: June 19th, 2011, 10:47 AM
  4. Traversing a min heap to get sorted
    By derek in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 20th, 2010, 04:17 PM
  5. traversing multiple jTabbedPane?
    By dewboy3d in forum AWT / Java Swing
    Replies: 3
    Last Post: October 2nd, 2009, 08:26 PM