Question

Write a method called inputStats that accepts a Scanner representing an input file and reports the...


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,

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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);

}

}

Add a comment
Know the answer?
Add Answer to:
Write a method called inputStats that accepts a Scanner representing an input file and reports the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Language: C Write an encoder and a decoder for a modified "book cipher." A book cipher...

    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 doc...

    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...

    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:...

  • In Java code Write a complete method name convertFile that accepts a File object representing an...

    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...

    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...

    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...

    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...

    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#

  • Need help understanding this question from CodeStepByStep in Java, any help is appreciated! Write a method...

    Need help understanding this question from CodeStepByStep in Java, any help is appreciated! Write a method named coinFlip that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of whitespace-separated tokens. Assume that the input file data represents results of sets of coin flips. A coin flip is either the letter H or T, or the word Heads or Tails, in either upper or lower case, separated by at...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT