Skip to content

Commit 5f3673f

Browse files
committed
Updates to FreqTableHandler
1 parent 76de7f8 commit 5f3673f

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

Lab_3/FreqTableHandler.java

+18-8
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,23 @@ public static PriorityQueue getFrequencies(String fileName){
2121
** number of lines in the frequency table (one per line).
2222
**
2323
** Entries in the frequency table must be in the form [A-Za-Z]\D*[0-9]+ and
24-
** failure to conform to this will cause the program to ignore that line
25-
** entirely. Note that \D in this context does not include negated underscore.
26-
** If a required character is skipped for this reason, it will cause the
27-
** incorrect code key to be generated and will most likely end up throwing an
28-
** EncodingException when the program goes to encode or decode the message.
24+
** failure to conform to this will generally cause an EncodingException to be
25+
** thrown, which will result in the program exiting prematurely.
26+
** Note that \D in this context does not include negated underscore.
27+
** We chose to exit the program rather than simply ignore that line because
28+
** it's unlikely that we'll be able to generate a correct code key if there
29+
** was an error in the frequency table that prevented us from reading data
30+
** on one of the keys. One way or another, this is almost certain to cause
31+
** an issue later when trying to encode or decode a message. So we cut things off
32+
** early rather than keep trudging down a futile path.
2933
**
3034
** @param fileName The name of the file containing the frequencyTable.
3135
**
3236
**/
3337
int totalChars = getNumLines(fileName);
34-
String[] characterLIst = new String[totalChars];
35-
int[] frequencyList = new int[totalChars];
36-
38+
39+
HuffmanNode[] codes = parseFile(fileName);
40+
3741
return new PriorityQueue(10);
3842

3943
}
@@ -127,6 +131,10 @@ public static HuffmanNode[] parseFile(String fileName) {
127131
// and add the node to codes[].
128132
} else if(character == '\n') {
129133

134+
// If we've parsed a line without getting data, just move on
135+
if(stringCharacter.equals("") && stringFrequency.equals("")){
136+
continue;
137+
}
130138

131139
// Error checking--Errors will throw an EncodingException. It's unlikely
132140
// that encoding/decoding will be successful if there's an error in the
@@ -146,6 +154,8 @@ public static HuffmanNode[] parseFile(String fileName) {
146154
errorString += stringCharacter + "\" contains more than one character.";
147155
throw new EncodingException(errorString);
148156
}
157+
158+
//
149159

150160
codes[nodeCounter] = new HuffmanNode(stringCharacter, frequency);
151161

0 commit comments

Comments
 (0)