Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

for Question 7 import java.io.File; import java.io.FileNotFoundException; import

ID: 3699556 • Letter: F

Question

for Question 7

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class CountWords {

   public static void main(String[]args)

           throws FileNotFoundException{

           Scanner console = new Scanner(System.in);

           Scanner input = getInput(console);

           int count = 0;

           while (input.hasNext()) {

               String word = input.next();

               count++;

           }

           System.out.println("total words =" + count);

   }

   public static Scanner getInput (Scanner console)

   throws FileNotFoundException{

       System.out.print("input file name?");

       File f = new File (console.nextLine());

       while(!f.canRead()) {

           System.out.println("File not found. Try agaon.");

           System.out.print("input file name?");

           f = new File (console.nextLine());

       }

       return new Scanner(f);

   }

          

   }

Question 7 1.5 pts Which of the following statements CORRECTLY describe the program CountWords listed on pp 421-422? Check all that apply. The program constructs two Scanner objects: one for interactive input and another one for file processing. O The program uses a File method called canRead to determine if the file exists and can be read. The program processes a file line-by-line. The header for the method getlnput reads: public static Scanner getlnput (Scanner console) throws FileNotFoundException The method getlnput returns a Scanner object associated with a specific file

Explanation / Answer

option 1 correct
as two object of Scanner console and input are created.
console is for userinput and input object handles the file

option 2 is correct
as f.fileRead() check if file can be opened and read and throw approprite exceptions accordingly

option 3 is not correct
as file uses hasNext() method of read file element by element


option 4 is correct
getInput() method handles the file opening and has correct header


option 5 is correct
as getInput() returns th object of file inputteed by user


Correct option: 1, 2, 4 and 5