I need to adjust this Huffman code so that it asks for the u

I need to adjust this Huffman code so that it asks for the user to input a text file. Here are the HuffmanTree and HuffmanNode classes as well as a sample text file that could be inputted:

HuffmanTree.java

HuffmanNode.java

Sample text file: EnglishAlphabet.txt

Solution

// HuffmanTree.java

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

public class HuffmanTree {

   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();
               prob = fin.nextDouble();
               nodes.add(findInsertIndex(nodes, prob), new HuffmanNode(symbol, prob));
           }
           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, node1, node;
           while (nodes.size() > 1) {
               node0 = nodes.get(0);
               node1 = nodes.get(1);
               node = new HuffmanNode(null, node0.getP() + node1.getP());
               node.setLeft(node0);
               node.setRight(node1);
               nodes.remove(0);
               nodes.remove(0);
               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{
           setCodeWords(root.getLeft(), codeWord + \"0\");
           setCodeWords(root.getRight(), codeWord + \"1\");
       }
   }

   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();
   }

}

// HuffmanNode.java

public class HuffmanNode {

   private String S;
   private double P;
   private String C;
   private int L;
   private HuffmanNode left, right;

   public HuffmanNode(String s, double p) {
       S = s;
       P = p;
   }

   public String getS() {
       return S;
   }

   public void setS(String s) {
       S = s;
   }

   public double getP() {
       return P;
   }

   public void setP(double p) {
       P = p;
   }

   public String getC() {
       return C;
   }

   public void setC(String c) {
       C = c;
   }

   public int getL() {
       return L;
   }

   public void setL(int l) {
       L = l;
   }

   public HuffmanNode getLeft() {
       return left;
   }

   public void setLeft(HuffmanNode left) {
       this.left = left;
   }

   public HuffmanNode getRight() {
       return right;
   }

   public void setRight(HuffmanNode right) {
       this.right = right;
   }
}

I need to adjust this Huffman code so that it asks for the user to input a text file. Here are the HuffmanTree and HuffmanNode classes as well as a sample text
I need to adjust this Huffman code so that it asks for the user to input a text file. Here are the HuffmanTree and HuffmanNode classes as well as a sample text
I need to adjust this Huffman code so that it asks for the user to input a text file. Here are the HuffmanTree and HuffmanNode classes as well as a sample text

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site