-
Notifications
You must be signed in to change notification settings - Fork 0
/
Words.java
64 lines (51 loc) · 2.61 KB
/
Words.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**COMP163 Common Words
Dr. Ken Williams
Dorian Holmes
11/09/2017
*/
import java.util.*;
import java.io.*;
public class Words {
public static void main (String[] unused)throws IOException {
UseCount[] wordArray = new UseCount [10000]; /*Creates an array that can hold 10,000 UseCount objects*/
int wordCount = 0; //Declare into to count word
java.util.Scanner keyboard = new java.util.Scanner(System.in); //Read keyboard input
System.out.println("Enter file"); // Output text
String file = keyboard.next(); //Alows user to input text
File data = new File(file);//Get file from folder
Scanner info = new Scanner(data) ; //declare as object in program
while(info.hasNext()){ //reads file
String word = info.next().toLowerCase(); //Changes file text to lower case
word = cleanWord(word); //Uses the cleanWord method on word
boolean tf = false; //Declare boolean
for( int i = 0; i < wordCount; i++){ // Search through each word and compare to words in array
if(word.equals(wordArray[i].getWord())){
wordArray[i].increment();
tf = true;
}
}
if(!tf){ //If not true creates new object for word and put in the array
UseCount x = new UseCount( word);
wordArray[wordCount] = x;
wordCount++;
}
}
java.util.Arrays.sort( wordArray, 0, wordCount ); //put words in descending order
for( int j = 0; j < 25; j++) //Outputs the first 25 words and number of times they appeared
System.out.println( wordArray[j].getCount()+ " \t" + wordArray[j].getWord());
}
private static String cleanWord( String dirty ) {
int numChar = dirty.length() - 1; // index of last character
while (!Character.isLetter(dirty.charAt(numChar))) { // if ends in non-letter
if (numChar == 0) { // if only one character
return null; // nothing left
}
dirty = dirty.substring(0,numChar); // remove last character
numChar--; // decrement last char index
}
while (!Character.isLetter(dirty.charAt(0))) { // if starts with non-letter
dirty = dirty.substring(1); // remove first character
}
return dirty;
}
}