PLEASE READ THE WHOLE THING. THERE IS A SHELL BELOW THAT WE MUST FOLLOW. INSTRUC
ID: 3816904 • Letter: P
Question
PLEASE READ THE WHOLE THING. THERE IS A SHELL BELOW THAT WE MUST FOLLOW.
INSTRUCTIONS:
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 output.
Y our 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
--------------------------------------------------------------------------------------------------------------------
TEXT FILE:
data.txt
data1.txt
data2.txt
---------------------------------------------------------------------------------------------------------------------
SHELL:
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
Here is the code for you:
import java.util.*;
import java.io.*;
public class Chapter6LabShell
{
public static void main(String[] args)throws FileNotFoundException
{
Scanner kb = new Scanner(System.in);
boolean repeat = true;
while (repeat)
{
//call the method getFile
Scanner sc = getFile(kb);
//call the method inputStats
inputStats(sc);
System.out.print("Do you have another file:yes/no? ");
String answer = kb.next();
kb.nextLine();
if(answer.equalsIgnoreCase("no"))
repeat = false;
}
}
/*This method asks the user for an output file name
cretaes an object of PritStream and returns it*/
public static PrintStream outputFile( ) throws FileNotFoundException
{
System.out.print("Enter the output file name: ");
String fileName = new Scanner(System.in).next();
PrintStream pw = new PrintStream(new File(fileName));
return pw;
}
/*this method asks the user to enter an input file name
as long the user entering a valid file name, the user must be prompted for a valid file nale
then a Acnner object will be cretaed abd returned*/
public static Scanner getFile(Scanner kb)throws FileNotFoundException
{
//Use files, data1.txt, data2.txt, data3.txt.
System.out.print("Enter the input file name: ");
String fileName = kb.next();
Scanner sc = new Scanner(new File(fileName));
return sc;
}
/*this method gets the input file name as its paramter
call the method outputFile to get the name of the output file
as long as there are lines in the input file
{
read one line
print the line in the output file
update the line count
call the method longestLine
call the method countTokens
call the method longest
output the result on the screen
}
output the longest line
*/
public static void inputStats(Scanner input)throws FileNotFoundException {
PrintStream ps = outputFile();
int lineCount = 0;
String longestLine = "";
int lineNumber = 1;
while(input.hasNextLine())
{
String line = input.nextLine();
ps.println(line);
lineCount++;
longestLine = longestLine(longestLine, line);
int numOfTokens = countTokens(line);
int longestWordLength = longest(line);
ps.println("Line " + lineNumber + " has " + numOfTokens + " tokens (longest = " + longestWordLength + ")");
}
ps.println("Longest line: " + longestLine);
}
/*gets two String and return the longest string*/
public static String longestLine(String longestLine, String line)
{
if(longestLine.length() > line.length())
return longestLine;
return line;
}
/*this method gets ons string of words seperated by spaces
and returns the number of the words(token) in the given String*/
public static int countTokens(String line)
{
StringTokenizer words = new StringTokenizer(line);
int wordsCount = 0;
while(words.hasMoreTokens())
{
String word = words.nextToken();
wordsCount++;
}
return wordsCount;
}
/*this methods gets a String and returns the length of the longest word in the String*/
public static int longest(String line)
{
StringTokenizer words = new StringTokenizer(line);
String word = words.nextToken();
int longestWordLength = word.length();
while(words.hasMoreTokens())
{
word = words.nextToken();
int length = word.length();
if(length > longestWordLength)
longestWordLength = length;
}
return longestWordLength;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.