Hi, I have been working on a project for my java programming class but I just dont know where to even start.

here is what i was assigned


Your program must:

  • Not declare any class attributes. You will not need them in a carefully designed class.
  • Read a single speech text file into an array of words, and then print to the screen the total number of words found in the file. For example, the Fort Bragg speech contains 3654 words.
  • Display to the screen the number of unique words as well as the percentage of unique words to total words. For example, the Fort Bragg speech contains 995 unique words, representing 27% of the total.
  • Prompt the user for any word, and then display the number of occurrences of that word in the speech.
  • Output a text file that consists of a listing of the unique words in the speech with the number of occurrences of each word following the word. The file lists the words starting with those occurring most often and ending with those occurring once only. If you have words that occur the same number of times list them in alphabetical order.

I'm not asking anyone to do my whole project ... i just need some help getting started.

For example i dont know how to read or write text files and i am not sure how to manipulate arrays of strings.

Any help would be greatly appreciated

Class attributes are attributes declared within the class but outside of all your methods ... for some reason my teacher doesnt want us to use these. I'm not sure yet if this is going to significantly complicate things ... I dont think it will make that much of a difference.

Thanks for the link ... i will look over that and see what i can do then i will ask again if i get stuck

For reading the file:

File file = new File("C:/myFileHere.txt");
        BufferedReader in = null;
        try
        {
            in = new BufferedReader(new FileReader(file));
 
            String line;
            //loop through each line in the file
            while ((line = in.readLine()) != null)
            {
                //split line up by any words separated by a space
                String[] words = line.split(" ");
                //get array count for total words on this line
                //loop through words and keep track of unique words with a hashmap
            }
        }
        catch (Exception e)
        {}

You can use HashMap to put each word in as a key. For each word you add, check to see if it's already a key. If so, increase the value of that key. This'll keep track of how many times a word shows up. The size of the hashmap will tell you how many unique words there are and the value of each key will tell you how many times that word shows up.

After that, the rest is just simple math.

This article has been dead for over six months. Start a new discussion instead.