CSC java chapter 6 Write a complete program that reads an input file and reports
ID: 3813972 • Letter: C
Question
CSC java chapter 6 Write a complete program that reads an input file and reports various statistics about the file's text. In particular, your program should report the number of lines in the file, the longest line, the number of tokens on each line, and the length of the longest token on each line. You may assume that the input file has at least one line of input and that each line has at least one token. A shell has been provided with all the needed methods. Make sure that your program generates the following output exactly. You must create a three different input files using the stories in the sample 1.- data.txt Beware the Jabberwock, my son, the jaws that bite, the claws that catch, Beware the JubJub bird and shun the frumious bandersnatch. 2.- data1.txt Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt 3.- data2.txt Imprimis: I am a man who, from his youth upwards, has been filled with a profound conviction that the easiest way of life is the best. Hence, though I belong to a profession proverbially energetic and nervous, output.-------------------------------------------------------------------------------------------- Your program should output the result on the screen and also in an output file. Your program must include data validation. The input file name that the user enters must be valid. Sample output: Enter the input file name: sadsd Enter the input file name: a.txt Enter the input file name: data.txt Enter the output file name: out.txt Beware the Jabberwock, my son, Line 1 has 5 tokens (longest = 11) the jaws that bite, the claws that catch, Line 2 has 8 tokens (longest = 6) Beware the JubJub bird and shun Line 3 has 6 tokens (longest = 6) the frumious bandersnatch. Line 4 has 3 tokens (longest = 13) Longest line: the jaws that bite, the claws that catch, ****************************** Do you have another file:yes/no? yes Enter the input file name: data1.txt Enter the output file name: out1.txt Lorem ipsum dolor sit amet, Line 1 has 5 tokens (longest = 5) consectetur adipisicing elit, Line 2 has 3 tokens (longest = 11) sed do eiusmod tempor incididunt Line 3 has 5 tokens (longest = 10) Longest line: sed do eiusmod tempor incididunt ****************************** Do you have another file:yes/no? Yes Enter the input file name: data2.txt Enter the output file name: out2.txt Imprimis: I am a man who, from his youth upwards, Line 1 has 10 tokens (longest = 9) has been filled with a profound conviction Line 2 has 7 tokens (longest = 10) that the easiest way of life is the best. Line 3 has 9 tokens (longest = 7) Hence, though I belong to a profession proverbially energetic and nervous, Line 4 has 11 tokens (longest = 12) Longest line: Hence, though I belong to a profession proverbially energetic and nervous, ****************************** Do you have another file:yes/no? no Good Bye
Explanation / Answer
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class FileStats {
public static void main(String[] args) {
String inputFile = null;
String outFile = null;
String longLine = null;
Scanner sc = new Scanner(System.in);
Scanner input = null;
int noOfLines = 0;
String tokens[] = null;
String line;
int tknLngLen = 0;
int noOfTokens = 0;
String output = "";
do {
noOfLines = 0;
noOfTokens = 0;
System.out.println("Enter the input file name:");
inputFile = sc.nextLine();
System.out.println("Enter the output file name:");
outFile = sc.nextLine();
output += "Input File: "+inputFile+" Output File: "+outFile+" ";
try {
input = new Scanner(new File(inputFile));
while(input != null && input.hasNext()) {
tknLngLen = 0;
line = input.nextLine();
if(line != null && !"".equals(line.trim())) {
noOfLines++;
//firstline
if(noOfLines == 1) {
longLine = line;
} else if(longLine.length() < line.length()) {
longLine = line;
}
System.out.println(line);
tokens = line.split(" ");
if(tokens != null) {
for(String tkn : tokens) {
if(tkn != null && !"".equals(tkn.trim())) {
if(tkn.trim().length() > tknLngLen)
tknLngLen = tkn.length();
noOfTokens++;
}
}
}
System.out.println("Line "+noOfLines+" has "+(tokens != null ? tokens.length : 0)+" tokens (longest = "+tknLngLen+")");
output +="Line "+noOfLines+" has "+(tokens != null ? tokens.length : 0)+" tokens (longest = "+tknLngLen+") ";
}
}
//System.out.println(" File: "+inFileName+" No of lines: "+noOfLines);
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("File not found");
}
System.out.println("Do you have another file:yes/no?");
String query = sc.nextLine();
if(query != null && "no".equalsIgnoreCase(query.trim()))
break;
} while(true);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
writer.write(output);
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
---output-----------------
Enter the input file name:
D: aviChegdatasample1.txt
Enter the output file name:
D: aviChegdataOutput1.txt
Beware the Jabberwock, my son,
Line 1 has 5 tokens (longest = 11)
the jaws that bite, the claws that catch,
Line 2 has 8 tokens (longest = 6)
Beware the JubJub bird and shun the frumious bandersnatch.
Line 3 has 9 tokens (longest = 13)
Do you have another file:yes/no?
no