I get this error when I run my Huffman code and Im hoping it

I get this error when I run my Huffman code and I\'m hoping it can be fixed:

Exception in thread \"main\" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
   at java.util.ArrayList.rangeCheck(ArrayList.java:653)
   at java.util.ArrayList.get(ArrayList.java:429)
   at com.company.HuffmanTree.createHuffmanTree(HuffmanTree.java:50)
   at com.company.HuffmanTree.main(HuffmanTree.java:28)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Here is my code. It consists of the classes HuffmanTree.java and HuffmanNode.java. I have also included a test text file.

//HuffmanNode.Java

//English.txt (symbols with probabilities)

Solution

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;

public class HuffmanTree {
static int degree=4;
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print(\"Enter the file name: \");

String fileName = in.nextLine();
ArrayList<HuffmanNode> nodes = new ArrayList<>();
ArrayList<HuffmanNode> temp = new ArrayList<>();
try {
Scanner fin = new Scanner(new FileReader(fileName));
String symbol;
double prob;
while(fin.hasNext()){
symbol = fin.next();
System.out.println(symbol);
prob = fin.nextDouble();
nodes.add(findInsertIndex(nodes, prob), new HuffmanNode(symbol, prob,degree));
}
           fin.close();
temp.addAll(nodes);
HuffmanNode root = createHuffmanTree(temp);
setCodeWords(root);
double avg = 0;
for (HuffmanNode node : nodes) {
System.out.println(node.getS() + \": \" + node.getC() + \": \" + node.getL());
avg += node.getL();
}
if(avg != 0)
avg /= nodes.size();
System.out.println(\"Average code length is: \" + avg);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

private static HuffmanNode createHuffmanTree(ArrayList<HuffmanNode> nodes){
HuffmanNode root = null;
if(nodes.size() > 0) {
HuffmanNode[] node0 = new HuffmanNode[degree] ;
while (nodes.size() > 1) {
              
          
double w =0.0;
for(int i=0;i<degree&& nodes.size()>0;i++) // add the condition node.size()>0 to breaK if there are no nodes left
{   
                   node0[i] =nodes.get(0);
nodes.remove(0);
w = w+node0[i].getP();}
HuffmanNode node = new HuffmanNode(null, w,degree);
for(int i=0;i<degree;i++)
{node.setNode(node0[i], i);}
nodes.add(findInsertIndex(nodes, node.getP()), node);
}
root = nodes.get(0);
}
return root;
}

private static void setCodeWords(HuffmanNode root){
if(root != null){
setCodeWords(root, \"\");
}
}

private static void setCodeWords(HuffmanNode root, String codeWord){
if(root.getS() != null){
root.setC(codeWord);
root.setL(codeWord.length());
}
else{
for(int i=0;i<degree;i++)
setCodeWords(root.getNode(i), codeWord + \"\"+i);

}
}

private static int findInsertIndex(ArrayList<HuffmanNode> nodes, double prob){
for(int i = 0; i < nodes.size(); ++i){
if(prob < nodes.get(i).getP()){
return i;
}
}
return nodes.size();
}

}

output:

C:> java HuffmanTree
Enter the file name: English.txt
a,
b,
c,
d,
e,
f,
g,
h,
i,
j,
k,
l,
m,
n,
o,
p,
q,
r,
s,
t,
u,
v,
w,
x,
y,
z,
z,: 2000: 4
q,: 2001: 4
x,: 2002: 4
j,: 2003: 4
k,: 201: 3
v,: 202: 3
b,: 203: 3
p,: 010: 3
y,: 011: 3
g,: 012: 3
f,: 013: 3
w,: 100: 3
m,: 101: 3
u,: 102: 3
c,: 103: 3
l,: 21: 2
d,: 22: 2
r,: 23: 2
h,: 30: 2
s,: 31: 2
n,: 32: 2
i,: 33: 2
o,: 00: 2
a,: 02: 2
t,: 03: 2
e,: 11: 2
Average code length is: 2.730769230769231

I get this error when I run my Huffman code and I\'m hoping it can be fixed: Exception in thread \
I get this error when I run my Huffman code and I\'m hoping it can be fixed: Exception in thread \
I get this error when I run my Huffman code and I\'m hoping it can be fixed: Exception in thread \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site