CSC java chapter 6 Write a complete program that reads an input file and reports
ID: 3813973 • 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
the jaws that bite, the claws that catch,
Beware the JubJub bird and shun
the frumious bandersnatch.
2.- data1.txt
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
package com.cheggtest;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Scanner;
public class ProcessFileData{
public static void main(String[] args) throws IOException {
File file = null;
BufferedReader bufferedReader = null;
String readLine = "";
String data = "";
String[] tokens = null;
BufferedWriter bw = null;
FileWriter fw = null;
int count = 0;
String yesOrNo = "";
try {
Scanner scanner = new Scanner(new InputStreamReader(System.in));
System.out.println("Enter How many files you want to process :");
int fileCount = scanner.nextInt();
while (count <=fileCount) {
if (count == fileCount) {
System.out.println("Do you have another file:yes/no? ");
yesOrNo = scanner.next();
if (yesOrNo != null && yesOrNo.equalsIgnoreCase("Yes"))
count = 0;
else {
System.out.println("Good Bye");
System.exit(0);
}
}
count++;
System.out
.println("Enter input file name with complete path :");
String fileName = scanner.next();
File f = new File(fileName);
if (f.exists() && !f.isDirectory()) {
} else {
System.out
.println("Enter input Valid file name with complete path :");
fileName = scanner.next();
}
System.out
.println("Enter output file name with complete path :");
String outFileName = scanner.next();
try {
file = new File(fileName);
bufferedReader = new BufferedReader(new FileReader(file));
fw = new FileWriter(outFileName);
bw = new BufferedWriter(fw);
} catch (Exception e) {
System.out.println("Please enter Valid file Name :");
}
String longestWord = "";
while ((readLine = bufferedReader.readLine()) != null) {
tokens = readLine.split("\s*(=>|,|\s)\s*");// \s*(=>|,|\s)\s*
// [\p{Punct}\s]
int wordLength = 0;
for (int i = 0; i < tokens.length; i++) {
String word = tokens[i];
if (wordLength > word.length()) {
} else {
wordLength = word.length();
longestWord = word;
}
}
System.out.println(readLine + " has tokens "
+ tokens.length + " longgest " + wordLength
+ " word is " + longestWord);
bw.write(readLine);
tokens = null;
}
bw.flush();
bw.close();
} //while (count >=fileCount);
// Files.write(Paths.get(outFileName), data.getBytes(),
// StandardOpenOption.CREATE);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
Enter How many files you want to process :
2
Enter input file name with complete path :
D:\Chegg\data2.txt
Enter output file name with complete path :
D:\Chegg\data2Op.txt
Lorem ipsum dolor sit amet, has tokens 5 longgest 5 word is dolor
consectetur adipisicing elit, has tokens 3 longgest 11 word is adipisicing
sed do eiusmod tempor incididunt has tokens 5 longgest 10 word is incididunt
Enter input file name with complete path :
D:\Chegg\data3.txt
Enter output file name with complete path :
D:\Chegg\data3Op.txt
Imprimis: I am a man who, from his youth upwards, has tokens 10 longgest 9 word is Imprimis:
has been filled with a profound conviction has tokens 7 longgest 10 word is conviction
that the easiest way of life is the best. has tokens 9 longgest 7 word is easiest
Hence, though I belong to a profession proverbially energetic and nervous, has tokens 11 longgest 12 word is proverbially
Do you have another file:yes/no?
no
Good Bye