Write a method called inputStats that accepts a Scanner
representing an input file and reports the number of lines, the
longest line, the number of tokens on each line, and the length of
the longest token on each line. If the file contains the following
text:
Beware the Jabberwock, my son, the jaws that bite, the claws that catch, Beware the JubJub bird and shun the frumious bandersnatch.
For the input above, your method should produce the following output:
Line 1 has 5 tokens (longest = 11) Line 2 has 8 tokens
(longest = 6) Line 3 has 6 tokens (longest = 6) Line 4 has 3 tokens
(longest = 13) Longest line: the jaws that bite, the claws that
catch,
CODE
import java.util.Scanner;
public class Main {
public void inputStats(Scanner sc) {
int numLines = 0;
int maxLineLength = 0;
String longestLine = "";
while(sc.hasNextLine()) {
numLines ++;
String line = sc.nextLine();
if (line.length() > maxLineLength) {
maxLineLength = line.length();
longestLine = line;
}
String[] tokens = line.split(" ");
int maxTokenLength = 0;
for (String token : tokens) {
if (token.length() > maxTokenLength) {
maxTokenLength = token.length();
}
}
System.out.format("Line %d has % tokens (longest = %d)\n", numLines, tokens.length, maxLineLength);
}
System.out.println("Longest Line: " + longestLine);
}
}
Write a method called inputStats that accepts a Scanner representing an input file and reports the...
Language: C Write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the text. The first number denotes the...
Language: C Write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the text. The first number denotes the...
Text book used: C Primer Plus 6th Edition, Stephen Prata Create count_ch() that will count all alphanumeric characters, all whitespace characters and all punctuation entered until the user simulates end of file from the keyboard. Also provide a total of ALL characters regardless of type. At the end, you should display 4 totals. (Also, review pages 252-254) If you copy and paste jabberwocky.txt, press enter, and then input the EOF simulation, you should have the following results... AlphaNumeric: 743, Punctuation:...
FIRST: Write code that prompts the user for the name of a text file, opens that file if it exists and reads the file one line at a time printing each line to the screen. You must implement the file read in a try/catch block rather than throwing the exception on the main method. NEXT: Modify your code so that the program takes the name of two files from the command line, opens the first file if it exists for...
In Java code Write a complete method name convertFile that accepts a File object representing an input text file and a PrintWriter objects as arguments. The method should read the contents of the input file and change all characters to uppercase and store the results in the output file.
Submit Chapter6.java with a public static
method named justifyText that accepts two parameters; a Scanner
representing a file as the first parameter, and an int width
specifying the output text width. Your method writes the text file
contents to the console in full justification form (I'm sure you've
seen this in Microsoft Word full.docx ). For example, if a Scanner is
reading an input file containing the following text:
Four score and
seven years ago our
fathers brought forth
on this...
Java: Chapter 4 Number 11:
11. Write a method called longestName that accepts a Scanner for the console and an integer n as parameters and IL Write a method calld longestHame tht acepts a scanner for the console and an ineger as araners and prompts for n names, then prints the longest name (the name that contains the most characters) in the format shown below, which might result from a call of longestName (console, 4): name #1? Roy name #2?...
The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...
Write a method called Drawline that accepts as input an integer n and generates a line of output in lstOutput with n hyphens. That is, if n = 6 we have a line of ‘------‘ displayed in the list box. Must be in C#