@@ -21,19 +21,23 @@ public static PriorityQueue getFrequencies(String fileName){
21
21
** number of lines in the frequency table (one per line).
22
22
**
23
23
** 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.
29
33
**
30
34
** @param fileName The name of the file containing the frequencyTable.
31
35
**
32
36
**/
33
37
int totalChars = getNumLines (fileName );
34
- String [] characterLIst = new String [ totalChars ];
35
- int [] frequencyList = new int [ totalChars ];
36
-
38
+
39
+ HuffmanNode [] codes = parseFile ( fileName );
40
+
37
41
return new PriorityQueue (10 );
38
42
39
43
}
@@ -127,6 +131,10 @@ public static HuffmanNode[] parseFile(String fileName) {
127
131
// and add the node to codes[].
128
132
} else if (character == '\n' ) {
129
133
134
+ // If we've parsed a line without getting data, just move on
135
+ if (stringCharacter .equals ("" ) && stringFrequency .equals ("" )){
136
+ continue ;
137
+ }
130
138
131
139
// Error checking--Errors will throw an EncodingException. It's unlikely
132
140
// that encoding/decoding will be successful if there's an error in the
@@ -146,6 +154,8 @@ public static HuffmanNode[] parseFile(String fileName) {
146
154
errorString += stringCharacter + "\" contains more than one character." ;
147
155
throw new EncodingException (errorString );
148
156
}
157
+
158
+ //
149
159
150
160
codes [nodeCounter ] = new HuffmanNode (stringCharacter , frequency );
151
161
0 commit comments