Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- /**
- *
- * @author Kaylie Lu
- * @period # 5
- */
- public class CountWords
- {
- private ArrayList <Word> myWords = new ArrayList <Word>();//ArrayList of Word objects
- private String fileName = "";//The name of the file read
- private String text = "";//The file in a String
- boolean hasError = false;
- int numberOfWords = 0;//The number of total words read
- int uniqueWords = 0;//The number of unique words there are
- /**
- * Loads the words from the text file and adds ot the ArrayList
- * @param fName
- */
- public CountWords(String fName){
- fileName = fName;
- loadFile(fName);
- addList(text);
- sort(myWords);
- }
- /**
- * Reads the file and stores the contents in a myWords ArrayList.
- * @param s The name of the file to be read.
- */
- public void loadFile(String s ){
- try{
- Scanner sc = new Scanner(new File(s));
- if (!sc.hasNextLine())
- throw new NoSuchElementException();
- while (sc.hasNextLine()) {
- String word = sc.nextLine();
- text = text + word + " ";
- }
- }catch(IOException i){
- System.out.println("File: " + fileName);
- System.out.println("Error:" + i.getMessage());
- hasError = true;
- }
- }
- /**
- * Private method to remove the punctuation from the list of words.
- * @param s
- */
- private String removePunc(String s){
- String copy = s;
- String newS= "";
- for (int i = 0; i < copy.length(); i++) {
- char loc = copy.charAt(i);
- if ((loc >= 'a' && loc <= 'z')||( loc >= 'A' && loc <= 'Z')
- || (loc >= '0' && loc <= '9')||(loc == (char)32 )||(loc == (char)39) )
- newS += loc;
- }
- return newS;
- }
- /**
- * Helper method adds words to the myWords list from a String
- * @param s the String to be divided into words
- */
- private void addList(String s){
- removePunc(s);
- String w = "";
- while ((!(text.indexOf(' ')==-1))&&(text.length() > 0)) {
- int checked,spaceIndex;
- checked= 0;
- spaceIndex = text.indexOf(' ');
- w = text.substring(0, spaceIndex);
- text = text.substring(spaceIndex + 1);
- if (w.indexOf("-") < 0) {
- Word currWord = new Word(w,0);
- if (!currWord.getString().equalsIgnoreCase("")) {
- for (int i = 0; i <myWords.size(); i++) {
- if (currWord.equals(myWords.get(i))) {
- (myWords.get(i)).incrementCount();
- numberOfWords++;
- checked++;
- }
- }
- if (checked == 0) {
- myWords.add(currWord);
- numberOfWords++;
- uniqueWords++;
- }
- }
- }
- }
- }
- /**
- * Prints the statistics of the myWords file.
- */
- public void printStats(){
- if (hasError == false){
- System.out.println("File: " + fileName);
- }
- System.out.println("Total number of unique words used in the file: " + uniqueWords);
- System.out.println("Total number of words in file: " + numberOfWords);
- System.out.println("Top 30 words are: ");
- int i = 0;
- for(int j = 0; j<myWords.size();j++){
- if(i<30)
- {
- System.out.printf("%2d%6d%12s",j,myWords.get(j).getCount(),myWords.get(j).getString());
- System.out.println();
- i++;
- }
- }
- reset();
- }
- /**
- * Selection sorts the arrayList of words
- * @param words ArrayList to be sorted
- */
- private void sort(ArrayList<Word> words) {
- for (int i = 1; i < words.size(); i++) {
- int pos = i;
- Word w = words.get(pos);
- while (pos > 0 && (words.get(pos - 1).getString().
- toLowerCase().compareTo(w.getString().toLowerCase())) < 0) {
- words.set(pos, words.get(pos - 1));
- pos--;}
- words.set(pos, w);
- }
- for (int i = 1; i < words.size(); i++) {
- int pos = i;
- Word w = words.get(pos);
- while (pos > 0 && words.get(pos - 1).compareCount(w) < 0) {
- words.set(pos, words.get(pos- 1));
- pos--;
- }
- words.set(pos, w);
- }
- }
- /**
- * Resets the variables for a new File
- */
- public void reset(){
- ArrayList <Word> myWords = new ArrayList<Word>();
- String fileName = "";
- String text = "";
- numberOfWords = 0;
- uniqueWords = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement