Question: Multiple choice question with explanation. Thanks! Consider the following code: public int getNumberOfLines (String filename) { Scanner fileIn = new Scanner(new FileInputStream(filename)); int count =
Multiple choice question with explanation. Thanks!
Consider the following code: public int getNumberOfLines (String filename) { Scanner fileIn = new Scanner(new FileInputStream(filename)); int count = 0; while(fileIn.hasNextLine()) { String tmp = fileIn.nextLine(); count += 1; } return count; } public void printNumberOfLines() { String filename = "MyFile.txt"; System.out.println("Number of lines: + getNumberOfLines(filename)); } Assume that the above methods are written inside a class FileLineCount, and that all necessary import statements have been included. When we compile this code, we get the following error: error: unreported exception FileNotFoundException; must be caught or declared to be thrown Which of the following approaches will get rid of this compile error? (Select all that apply) adding throw new FileNotFoundException(); as the second line in the getNumberofLines method, immediately after the Scanner object is created. adding throws FileNotFoundException to the declaration of getNumberoflines method, and to the declaration of the printNumberOfLines method, and to the declaration of any other method that called either of these methods. putting the Scanner object creation inside a try block (with a corresponding catch block) in the getNumberOfLines method. Creating the FileInputStream object on its own line in the printNumberOfLines method and then passing it as a parameter to the Scanner constructor
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
