Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

this is my main not attaching the rest of the code since its compiling. when i c

ID: 3551935 • Letter: T

Question

this is my main not attaching the rest of the code since its compiling.

when i compiles, it comiples. after putting the file name and pick the task from the menu,

menu a throws,


a

Exception in thread "main" java.util.NoSuchElementException: No line found

at java.util.Scanner.nextLine(Scanner.java:1585)

at n00679229.main(n00679229.java:364)


b

Character Code

Exception in thread "main" java.lang.NullPointerException

at Huffman.displayAGCodeTable(n00679229.java:255)

at n00679229.main(n00679229.java:375)




and when i put c it does not create the table i am assuming since its not reading the file correctly.


samething goes to D

d

the coded message decoded:

Exception in thread "main" java.lang.NullPointerException

at Huffman.decodeMessage(n00679229.java:314)

at Driver.main(Driver.java:645)



please help.


======================================================================================









public class n{

private static void displayMenu(){

System.out.println("Please choose one of the tasks to perform: ");

System.out.println("a. Create the Huffman Tree");

System.out.println("b. Display the code table");

System.out.println("c. Display binary encoding of the file");

System.out.println("d. Display the decoded message");

System.out.println("a. Exit");

}

public static void main(String args[]) throws FileNotFoundException

{

Scanner in = new Scanner(System.in);

String inputt,codedMessage = null,message = "";

Huffman code = new Huffman();

System.out.print("Please Enter the File Name : ");

String fileName = in.next();

File file = new File(fileName);

Scanner sc = new Scanner(file);

  

  

char choice='a';

while(choice != 'e'){

displayMenu();

choice = in.next().charAt(0);

switch(choice){

case 'a':

inputt= sc.nextLine();

while (inputt.compareTo("---")!=0)

{

message += inputt+"";

inputt = sc.nextLine();

}

code.createCodeTable(message);

System.out.println("The tree created:" );

  

code.displayHuffmanTree();

break;

case 'b': code.displayAGCodeTable();

break;

case 'c': codedMessage = code.encodeAGMessage(message);

break;

case 'd': System.out.println("the coded message decoded:");

System.out.println(code.decodeMessage(codedMessage));

break;

case 'e': break;

default: System.out.println("Invalid choice! Please select (a-d)");

break;

}

}

sc.close();

in.close();

}

}//End of Driver Class

Explanation / Answer

The problem that you have is that the code does not stop reading next line from the file.

Since, you have set the loop condition as : inputt.compareTo("---")!=0


The file which you are giving as input should have the last line as ---

Only those three characters without any white spaces.


Hope this works!


PS : I have tried to solve this question in your previous post and encountered the same error and fixed it in the same way, even though I made the working code by putting a lot of time another guy posted the answer before me, my time has been wasted :( , hope I gain some points from this question