This repository was archived by the owner on Sep 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordCounter.java
89 lines (58 loc) · 2.09 KB
/
WordCounter.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class WordCounter {
static myHashTable table = new myHashTable(40); //default length of 10
//main method for WordCounter
//handles/directs everything
//gives instructions on building the table, printing it, and giving the output
//the only callable method
public static String wordCount(String input_file, String output_file){
File inFile = new File(input_file);
File outFile = new File(output_file);
String status="";
status=buildHashTable(inFile); //creates table
if(!status.equals("Success")) return status;//returns a failure
status=createOutputFile(outFile);//outputs the table to a file
if(!status.equals("Success")) return status;//returns a failure
return "Okay; "+table.getStatistics();
}
//creates the table based on the input file
//runs insert on each word delimited by non-alphabetical characters
private static String buildHashTable(File inputFile){
String line;
try {
Scanner fileScanner = new Scanner(inputFile);
while(fileScanner.hasNext()){ //while the file has text
line = fileScanner.nextLine(); //gets the line
// runs insert on each word
//word being defined as characters separated
//by non alphabetical characters
for(String word:line.split("[^a-zA-Z]")){
if(!word.equals(""))//if the word is not empty
table.insert(word.toLowerCase(),1);
}
}
fileScanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return "Input File error";
}
return "Success";
}
//creates the output file based on hash table toString
private static String createOutputFile(File outputFile){
try {
BufferedWriter fileWriter = new BufferedWriter(new FileWriter(outputFile));
fileWriter.write(table.toString());
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
return "Output File error";
}
return "Success";
}
}