944,017 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1447
  • Java RSS
Oct 10th, 2006
0

I need a some help!!

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
silentcreeper is offline Offline
5 posts
since Oct 2006
Oct 11th, 2006
0

Re: I need a some help!!

Umm.. what are you refering to when you say "class attributes"? Are you refering to instance variables?
The following page has an example of how to read from text files: http://www.javacoffeebreak.com/faq/faq0004.html
Try it, then ask a more specific question about arrays of strings.
Last edited by Rayhan Muktader; Oct 11th, 2006 at 1:26 am.
Reputation Points: 28
Solved Threads: 3
Light Poster
Rayhan Muktader is offline Offline
30 posts
since Oct 2006
Oct 11th, 2006
0

Re: I need a some help!!

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
silentcreeper is offline Offline
5 posts
since Oct 2006
Oct 11th, 2006
0

Re: I need a some help!!

For reading the file:
Java Syntax (Toggle Plain Text)
  1. File file = new File("C:/myFileHere.txt");
  2. BufferedReader in = null;
  3. try
  4. {
  5. in = new BufferedReader(new FileReader(file));
  6.  
  7. String line;
  8. //loop through each line in the file
  9. while ((line = in.readLine()) != null)
  10. {
  11. //split line up by any words separated by a space
  12. String[] words = line.split(" ");
  13. //get array count for total words on this line
  14. //loop through words and keep track of unique words with a hashmap
  15. }
  16. }
  17. catch (Exception e)
  18. {}


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.
Last edited by Phaelax; Oct 11th, 2006 at 2:54 pm.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: NumberFormat - Incorrect rounding
Next Thread in Java Forum Timeline: set time in int





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC