Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.cheesekeg.wordcount;
- import java.util.ArrayList;
- import com.cheesekeg.wordcount.gui.WordCountWindow;
- /**
- * A Java Swing application that takes a given input from a TextArea and sorts space-separated words by popularity.
- * @author Brandon DeRosier
- * @since 0.1
- * @version 0.1
- */
- public class WordCount {
- WordCountWindow gui; // The jFrame I/O.
- /**
- * Construct a new WordCount.
- */
- private WordCount() {
- gui = new WordCountWindow();
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- new WordCount();
- }
- public static String CountWordString(String words) {
- ArrayList<WordEntry> wordList = CountWords(words);
- String result = "";
- for (int i = 0; i < wordList.size(); i++) {
- result += wordList.get(i) + "\n";
- }
- return result;
- }
- public static ArrayList<WordEntry> CountWords(String words) {
- ArrayList<WordEntry> wordList = new ArrayList<WordEntry>();
- String word = "";
- char buffer;
- for (int i = 0; i < words.length(); i++) {
- buffer = words.charAt(i);
- if (buffer == ' ' || buffer == '.' || buffer == ',' || buffer == '\n') {
- if (word.length() > 0) {
- WordEntry test = new WordEntry("");
- for (int j = 0; j < wordList.size(); j++) {
- if (wordList.get(j).GetWord().toLowerCase().equals(word.toLowerCase())) {
- test = wordList.get(j);
- break;
- }
- }
- if (test.GetWord() == "") {
- test.SetWord(word);
- wordList.add(test);
- }
- test.IncrementCount();
- word = "";
- }
- } else {
- word += buffer;
- }
- }
- // Simple descending sort.
- ArrayList<WordEntry> result = new ArrayList<WordEntry>();
- while (wordList.size() > 0) {
- WordEntry most = wordList.get(0);
- for (int j = 1; j < wordList.size(); j++) {
- if (wordList.get(j).GetCount() > most.GetCount()) {
- most = wordList.get(j);
- }
- }
- wordList.remove(most);
- result.add(most);
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement