I want my code to read the input file, line by line When I use the while loop, it only read the second line this the cod

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

I want my code to read the input file, line by line When I use the while loop, it only read the second line this the cod

Post by answerhappygod »

I want my code to read the input file, line by line
When I use the while loop, it only read the second line this the
code:
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;
public class Tester2 {
/**
* main method
* @param args the args from JVM
*/
public static void main(String args[]) throws
IOException{
Scanner sc= new Scanner(System.in);
//System.in is a standard input stream

//For holding string
String test = "";
//Reading data from file rather than
from input - Assuming input file is data.txt
try {
//Scanner object to read
data from file
Scanner fileReader = new
Scanner(new File("filetoencode.txt")); //System.in is a standard
input stream

//Reading data from
file
while
(fileReader.nextLine() != null){
test =
fileReader.nextLine();
}

//Closing file
reader
fileReader.close();
}
catch(Exception ex) {

System.out.println(ex);
}
// System.out.print("You have entered: "+
test);
//String test = "";// THE TESTING
STRING!!!
System.out.println("Testing String: " +
test);
int size = 0;
try {
size =
test.getBytes("utf8").length * Byte.SIZE;//SIZE OF THE STRING IN
BIT
} catch (UnsupportedEncodingException e)
{
System.out.println("Not
proper encoding...");
}
HuffmanFrequencyTable test2 = new
HuffmanFrequencyTable(test);//BUILD HUFFMAN TABLE
HuffmanTree test3 = new
HuffmanTree(test2.getTable());// BUILD HUFFMAN TREE
Encoder tst4 = new Encoder(test3,
test2.getTable());// ENCODING
String test5 =
tst4.result(test);//ENCODING RESULT
tst4.print();//PRINT FREQUENCY TABLE
WITH CODE FOR EACH CHARACTER
int sizeb = test5.length();//SIZE IN BIT
OF ENCODED STRING
System.out.println("Encoded bit
stream:");
System.out.println(test5);
System.out.println("Total number of bits
without Huffman coding: " + size);
System.out.println("Total number of bits
with Huffman coding: " + sizeb);
System.out.println("Decoded String: " +
Decoder.decode(test5, test3));

}
}
This code only read the first line
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;
public class Tester2 {
/**
* main method
* @param args the args from JVM
*/
public static void main(String args[]) throws
IOException{
Scanner sc= new Scanner(System.in);
//System.in is a standard input stream

//For holding string
String test = "";
//Reading data from file rather than
from input - Assuming input file is data.txt
try {
//Scanner object to read
data from file
Scanner fileReader = new
Scanner(new File("filetoencode.txt")); //System.in is a standard
input stream

//Reading data from
file

test =
fileReader.nextLine();


//Closing file
reader
fileReader.close();
}
catch(Exception ex) {

System.out.println(ex);
}
// System.out.print("You have entered: "+
test);
//String test = "";// THE TESTING
STRING!!!
System.out.println("Testing String: " +
test);
int size = 0;
try {
size =
test.getBytes("utf8").length * Byte.SIZE;//SIZE OF THE STRING IN
BIT
} catch (UnsupportedEncodingException e)
{
System.out.println("Not
proper encoding...");
}
HuffmanFrequencyTable test2 = new
HuffmanFrequencyTable(test);//BUILD HUFFMAN TABLE
HuffmanTree test3 = new
HuffmanTree(test2.getTable());// BUILD HUFFMAN TREE
Encoder tst4 = new Encoder(test3,
test2.getTable());// ENCODING
String test5 =
tst4.result(test);//ENCODING RESULT
tst4.print();//PRINT FREQUENCY TABLE
WITH CODE FOR EACH CHARACTER
int sizeb = test5.length();//SIZE IN BIT
OF ENCODED STRING
System.out.println("Encoded bit
stream:");
System.out.println(test5);
System.out.println("Total number of bits
without Huffman coding: " + size);
System.out.println("Total number of bits
with Huffman coding: " + sizeb);
System.out.println("Decoded String: " +
Decoder.decode(test5, test3));

}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply