Write a static method named textCount that takes a Scanner representing a file a
ID: 3659381 • Letter: W
Question
Write a static method named textCount that takes a Scanner representing a file as a parameter, and that reports various statistics about the file. In particular, your method should report the number of lines in the file, the length and text of the longest line. You may assume that the input file has at least one line of input. For example, 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." The method should produce the following output: Total lines = 4 Length of longest line = 41 Longest line = the jaws that bite, the claws that catch,Explanation / Answer
public static void textCount(Scanner input) { int numLines = 0; int numChars = 0; String maxLine = ""; while (input.hasNextLine()) { String next = input.nextLine(); numChars += next.length(); numLines++; if (next.length() > maxLine.length()) { maxLine = next; } } System.out.println("Total lines = " + numLines); System.out.println("Total chars = " + numChars); System.out.println("Length of longest line = " + maxLine.length()); System.out.println("Longest line " + maxLine); }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.