| | |
I need a some help!!
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2006
Posts: 5
Reputation:
Solved Threads: 0
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:
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
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.
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
•
•
Join Date: Oct 2006
Posts: 30
Reputation:
Solved Threads: 3
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.
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.
•
•
Join Date: Oct 2006
Posts: 5
Reputation:
Solved Threads: 0
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
Thanks for the link ... i will look over that and see what i can do then i will ask again if i get stuck
•
•
Join Date: Mar 2004
Posts: 765
Reputation:
Solved Threads: 38
For reading the file:
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.
Java Syntax (Toggle Plain Text)
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.
Last edited by Phaelax; Oct 11th, 2006 at 2:54 pm.
![]() |
Other Threads in the Java Forum
- Previous Thread: NumberFormat - Incorrect rounding
- Next Thread: set time in int
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows





