4 Replies - 6930 Views - Last Post: 29 July 2011 - 03:04 AM Rate Topic: -----

#1 Straydog77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-July 11

Unique words from text file, using ArrayList

Posted 28 July 2011 - 07:05 PM

I'm working on the following, and have the bulk of it done:
Write an application class named OrderedSetTest to do the following:
1. Create an OrderedSet (i.e., an instance of your OrderedSet class).
2. Use a Scanner for standard input to prompt for an input text file name.
3. Create a Scanner for that file.
4. Use the useDelimiter(..) Scanner method to make a sequence of 1 or more the following delimit the "words" in the text file:
5. white space characters
6. .,!?:;
7. digits (i.e., characters 0 - 9)

8. Using the Scanner's next() method read each word from the file; convert the word to lower case and add it to the OrderedSet.
While doing this, update a counter for the total number of words read.
9. Finally print the total number of words read, the number of unique words and the list of the unique words in alphabetical order.
Sample Input and Output
Input file:
The bigger they are,
the bigger they fall!

Execution:
There were 5 unique words and a total of 8 words in the file.
The 5 unique words in sorted order:
1. 'are'
2. 'bigger'
3. 'fall'
4. 'the'
5. 'they'

I'm having a problem adding all words to the OrderedSet while also creating a separate unique set. I'm also not entirely sure how to alphabetically sort my unique word set once I get there. Here is my code so far, the problem is in lines 27-35 but I can't quite seem to work this out. I also wondered if my approach is wrong and if I should just create one OrderedSet and somehow sort out unique words?
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;

public class OrderedSetTest
{
   public static void main(String [] args)
   {
	    OrderedSet words = new OrderedSet();
	    OrderedSet unique = new OrderedSet();
	    int counter = 0;
	 	Scanner console = new Scanner(System.in);
	    Scanner in = null;
	    String fileName;

	    System.out.printf("Enter the filename:\t");
	    fileName = console.nextLine();
	    try {
	      in = new Scanner(new File(fileName));
	    } catch(FileNotFoundException e) {
	      System.out.printf("Cannot open input file %s\n", fileName);
	      System.exit(1);
	    }
	    //in.useDelimiter("[- \r\t\n.?!,:;]+");
	    // in.useDelimiter("[-\\s.?!,:;]+");
	    // in.useDelimiter("[^-a-zA-Z_0-9]+");
	    in.useDelimiter("\\W+");
	    while(in.hasNext()) {
			words.add(in.next().toLowerCase());
	    	counter++;
			String uniqWord = (in.next().toLowerCase());
		if(!words.contains(uniqWord)){
			unique.add(uniqWord);
		}
		}//end while
	      System.out.println("There were " +unique.size()+ " unique words and a total of " +counter+ " words in the file.");
	      System.out.println("The " +unique.size()+ " unique words in sorted order:");
	      for (int i = 0; i < unique.size(); i++)
	      System.out.println(unique.get(i));
   }//end main
}//end OrderedSetTest class



Is This A Good Question/Topic? 0
  • +

Replies To: Unique words from text file, using ArrayList

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Unique words from text file, using ArrayList

Posted 28 July 2011 - 08:47 PM

First I am afraid if your OrderedSet has add() method? I think it should be an instance of ArrayList. You need an arrayList somewhere to archieve this,you may either create an instance variable of type arrayList and create add() method to add words in arrayList or you maycreate them in main and use them like those two of your objects.
Then approach is wrong here(assume these objects accept add() method by any way):
 while(in.hasNext()) {
			words.add(in.next().toLowerCase());
	    	counter++;
			String uniqWord = (in.next().toLowerCase());
		if(!words.contains(uniqWord)){
			unique.add(uniqWord);
		}
		} 

First you read a word and add it to the word list.You dont need to call .next() again to check if that word is in the list or not, because it takes another work. So will be skipping some words from test

EDIT: We need to see the orderSet class and tell us what error do you have exactly

This post has been edited by smohd: 28 July 2011 - 08:51 PM

Was This Post Helpful? 1
  • +
  • -

#3 Straydog77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-July 11

Re: Unique words from text file, using ArrayList

Posted 28 July 2011 - 10:34 PM

Here is the OrderedSet class, with an add method (and yes, some text was getting skipped over):
import java.util.ArrayList;

/**
   Initial specification of the OrderedSet class.
 */
public class OrderedSet
{
  /**
   * Initializes the ArrayList member, store, to be an initially empty
   * ArrayList of Strings.
   */
  ArrayList<String> store = new ArrayList<String>();
  public OrderedSet()
  {
  }
  /**
   * Adds the item s to the store at the correct position
   * in the natural ordering.
   * If s is already in the the store, then do nothing;
   * duplicates should not be added.
   * @param s the item to be added.
   */
  public void add(String s)
  {
	store.add(s);
  }

  /**
   * Get the number of items in the store.
   * @return the number of items in the store.
   */
  public int size()
  {
	return store.size();
  }

  /**
   * Retrieves the i-th item from the ordered set.
   * @param i the position, 0 to size() - 1 of the item
   * in the ordered set to be retrieved.
   * @throws IndexOutOfBoundsException if i < 0 or >= size()*/
  public String get(int i)
  {
	String one = store.get(i);
	if (i < 0 || i >= store.size())
	throw new IndexOutOfBoundsException();
	else
	return one;
  }

  /**
  * Determines if item s is in the ordered set.
  * @param s the item to be searched for.
  * @return true if s is in the ordered set or else false.
  */
  public boolean contains(String s)
  {
     return store.contains(s);
  }

  /**
   * Delete item s from the ordered set if it is present.
   * @param s the item to be deleted.
   * @return true if the item was present or else false.
   */
  public boolean remove(String s)
  {
	return store.remove(s);
  }

}


Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Unique words from text file, using ArrayList

Posted 29 July 2011 - 12:34 AM

That part which I showed first should be like:
 while (in.hasNext()) {
            String uniqWord = (in.next().toLowerCase());
            if (!words.contains(uniqWord)) {
                unique.add(uniqWord);
            }
            words.add(uniqWord);
            counter++;
        }//end while 

Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: Unique words from text file, using ArrayList

Posted 29 July 2011 - 03:04 AM

You might like to look at a binary search approach, which will give you O(log n) time as opposed to your current O(n) time for looking for duplicates. You can adapt (as it currently allows dups, which you don't want) the following perhaps:

import java.util.*;


public class SortedList<E extends Comparable<E>> extends LinkedList<E> {
    public boolean add(E e) {
        int index = Collections.binarySearch(this, e);

        if (index < 0) {
            super.add(-index - 1, e);
        } else {
            super.add(index, e);
        }

        return true;
    }

    public boolean addAll(Collection<?extends E> c) {
        for (Iterator<?extends E> i = c.iterator(); i.hasNext();)/> {
            this.add(i.next());
        }

        return true;
    }

}

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1