Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- //ANDY ZHANG, 2
- public class CountWords
- {
- ArrayList <String> list = new ArrayList <String> ();
- ArrayList <String> listCopy = new ArrayList <String> ();
- ArrayList <String> word = new ArrayList <String> ();
- ArrayList <Integer> wordCount = new ArrayList <Integer> ();
- int count;
- public void countAndStore()
- {
- count = 0;
- try {
- Scanner in = new Scanner(new File("Lincoln - a19.3.txt"));
- while(in.hasNext())
- {
- String temp = in.next();
- temp = temp.replaceAll("[^a-zA-Z ]", "").toLowerCase();
- if(temp.length() >= 1)
- {
- //System.out.println(temp);
- list.add(temp);
- listCopy.add(temp);
- count++;
- }
- else
- {
- //nothing
- }
- }
- } catch (Exception i) {
- System.out.println(i);
- }
- System.out.println(count);
- for(String s: list)
- {
- System.out.print(s + " ");
- }
- }
- public void countIndividual()
- {
- while(list.size() > 0)
- {
- int index = 0;
- int counter = 1;
- String store = list.get(0);
- //
- word.add(store);
- System.out.println(store);
- list.remove(index);
- while(index < list.size())
- {
- String temp = list.get(index);
- if(temp.equals(store))
- {
- counter++;
- list.remove(index);
- }
- index++;
- }
- wordCount.add(counter);
- }
- }
- public void search(String word)
- {
- int c = 0;
- for(int x = 0; x < listCopy.size(); x++)
- {
- String temp = listCopy.get(x);
- if(temp.equalsIgnoreCase(word))
- System.out.print("True with " + c + " searches of sequential");
- else
- System.out.print("False, word does not exist");
- c++;
- }
- System.out.println();
- }
- public void printer()
- {
- System.out.println();
- for(int x = 0; x < 30; x++)
- {
- System.out.printf("%-14s", word.get(x));
- System.out.print(" : ");
- System.out.printf("%s", wordCount.get(x));
- System.out.println();
- }
- }
- public void top()
- {
- int max;
- String temp;
- int temp1;
- for (int outer = 0; outer < word.size() - 1; outer++){
- max = outer;
- for (int inner = outer + 1; inner < word.size(); inner++){
- if (wordCount.get(inner) > wordCount.get(max)) {
- max = inner; // a new smallest item is found
- }
- }
- //swap word[outer] & word[max]
- temp = word.get(outer);
- word.set(outer, word.get(max));
- word.set(max, temp);
- //
- temp1 = wordCount.get(outer);
- wordCount.set(outer, wordCount.get(max));
- wordCount.set(max, temp1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement