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

Write a program that prompts the user to enter a filename (use a Scanner to get

ID: 3854281 • Letter: W

Question

Write a program that prompts the user to enter a filename (use a Scanner to get the filename). The program then outputs to the screen the number of lines, words and characters in the file. C:usersaronDesktop>java question1 Enter input file name: text1.txt 32 lines 159 words 810 characters. Collect the filename using a JFileChooser instead of using a Scanner. (Not shown here.) Also prompt the user to enter a string to search for in the file. In your output, state how many lines contained the requested string and display the lines and their line number. C:usersaronDesktop>java question1 Enter input file name: text1.txt Enter a string to search for: and 33 lines 159 words 810 characters. Your requested word - and - was found 8 times. 6: And what to do and how to be 10: But there in the sandpile at Sunday school. 21: Wash your hands before you eat. 23: Warm cookies and cold milk are good for you. 25: Learn some and think some 26: And draw and paint and sing and dance 27: And play and work everyday some. 31: Hold hands and stick together.

Explanation / Answer

Question1.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class Question1 {

  
   public static void main(String[] args) throws FileNotFoundException {
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter input file name: ");
       String fileName = scan.next();
       System.out.println("Enter a string to search for: ");
       String searchWord = scan.next();
       int charCount = 0;
       int wordCount = 0, lineCount = 0;
       Scanner fileScan = new Scanner(new File(fileName));
       while(fileScan.hasNextLine()){
           lineCount++;
           String line = fileScan.nextLine();
           charCount = charCount + line.length();
           wordCount = wordCount+line.split(" ").length;
       }
       System.out.println(lineCount+" lines.");
       System.out.println(wordCount+" words.");
       System.out.println(charCount+" characters.");
       System.out.println();
       int count = 0;
       Scanner fileScan1 = new Scanner(new File(fileName));
       while(fileScan1.hasNextLine()){
           String line = fileScan1.nextLine();
           if(line.toLowerCase().indexOf(searchWord.toLowerCase()) != -1){
               System.out.println(line);
               count++;
           }
       }
       System.out.println("Yor requested word - "+searchWord+" - "+"was found "+count+" times");
      
   }

}

Output:

Enter input file name:
D:\words.txt
Enter a string to search for:
dachshunds
3 lines.
18 words.
98 characters.

Sophie Sally and Jack were dachshunds
Dachshunds are the best dogs and all dogs
Yor requested word - dachshunds - was found 2 times

words.txt

Sophie Sally and Jack were dachshunds
Dachshunds are the best dogs and all dogs
are better than cats

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote