Computer Science Canada

Map/ArrayList Help

Author:  blackhawk_prince [ Sat Mar 08, 2008 6:50 pm ]
Post subject:  Map/ArrayList Help

I have the following assignment form school:
There are a number of historical pieces of writing where the author is not known with 100% certainty. One way to try to prove a particular author did or did not write a particular piece is to analyze the frequency that each word is used. Write a program that takes a filename as an argument and displays a list of all of the words used in the file and the percentage of the file that each word represents. This list must be sorted in descending percentages.
My teacher told us to map the word onto a custom class that can be later sorted. So I wrote the following:

Java:
import java.util.*;
 import java.io.*;
 public class Text{    
        public static void main (String args []){
               Map <String, Word> t = new HashMap  <String, Word>();
               try {
            Scanner fileIn = new Scanner(new File("story.txt"));
            while (fileIn.hasNext ()){
                String sym = fileIn.next ();
                if (t.containsKey (sym)){
                  t.get (sym).add ();
                } else {
                        Word newSym = new Word (1, sym);
                        t.put (sym, newSym);
                }
            }
         System.out.println (t.values());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        ArrayList <Word> stuff = new ArrayList <Word> (t.values ());
        ArrayList <Word> sorted = new ArrayList <Word> (Collections.sort (stuff));
        for (int i = 0; i < sorted.size (); i++){
                sorted.get (i).out ();
        }
        //System.out.println (sortedStuff);
        }
 }
 
class Word implements Comparable {
        private int times;
        private String name;
        public Word (int x, String y){
                times = x;
                name = y;
        }
        public void out (){
                System.out.println ("word: " + name + " repeats: " + times);
        }
        public void add (){
                times ++;
        }
        public int compareTo(Object o){
                Word other = (Word) o;
                return this.times - other.times;
        }
}


I keep getting an error on line 22:

code:
N:\Computer Science\Colloctions\Text.java:29: 'void' type not allowed here
        ArrayList <Word> sorted = new ArrayList <Word> (Collections.sort (stuff));

How do i fix this? I alos attached the code too.

Author:  HeavenAgain [ Sat Mar 08, 2008 9:29 pm ]
Post subject:  RE:Map/ArrayList Help

the sort method is a void method... and so you cannot pass that as a parameter for initial size of your arraylist
and after you sort it, since its a reference, your "stuff" variable is automatically sorted, and so the "sorted" variable is not necessary

Author:  wtd [ Sun Mar 09, 2008 2:13 am ]
Post subject:  RE:Map/ArrayList Help

Java:
class Word implements Comparable {
        private int times;
        private String name;
        public Word (int x, String y){
                times = x;
                name = y;
        }
        public void out (){
                System.out.println ("word: " + name + " repeats: " + times);
        }
        public void add (){
                times ++;
        }
        public int compareTo(Object o){
                Word other = (Word) o;
                return this.times - other.times;
        }
}


There are a few things you could be doing better here, though not related to the error you received.

You use generics earlier, so apply those to your code.

Java:
class Word implements Comparable<Word> {
        private int times;
        private String name;

        public Word (int x, String y){
                times = x;
                name = y;
        }

        public void out (){
                System.out.println ("word: " + name + " repeats: " + times);
        }

        public void add (){
                times ++;
        }

        @Override
        public int compareTo(Word w){
                return this.times - w.times;
        }
}


And instead of a void method called "out," let's override the toString method. Oh, and we'll give "add" a better name.


Java:
class Word implements Comparable<Word> {
        private int times;
        private String name;

        public Word (int x, String y){
                times = x;
                name = y;
        }

        @Override
        public String toString() {
                return "word: " + name + " repeats: " + times;
        }

        public void incrementTimesEncountered() {
                times++;
        }

        @Override
        public int compareTo(Word w) {
                return this.times - w.times;
        }
}

Author:  blackhawk_prince [ Mon Mar 10, 2008 10:28 am ]
Post subject:  RE:Map/ArrayList Help

Thanks alot, I got my code to work now.


: