Question: Current code: import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReadFile { public static void main(String[] args) { Scanner fileInput = getInput(); while (fileInput.hasNextLine()) {
Current code:
import java.io.File; import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFile {
public static void main(String[] args) { Scanner fileInput = getInput(); while (fileInput.hasNextLine()) { String s = fileInput.nextLine(); System.out.println(s); } } public static Scanner getInput () { Scanner console = new Scanner(System.in); // for keynoard input Scanner result = null; // for reading from a file while (result == null) { System.out.print("Please enter a filename : "); File someFile = new File(console.nextLine()); try { result = new Scanner(someFile); } catch (FileNotFoundException e) { System.out.println("File not found! Please try again."); } } return result; }
}
-----------------------------------------------
Make the following changes to the program:
1) add code to the program so that it counts the number of lines in the file that is read by the program
2) add code to the program so that it keep track of the longest line in the file (the line containing the most characters).
3) add code to the program to print the total number of lines and the number of characters in the longest line after the program displays the lines in the file.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
