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

Those are the text files the user has to enter does math.randon Home Chegg.com s

ID: 3727261 • Letter: T

Question


Those are the text files the user has to enter does math.randon Home Chegg.com s/a3-w18%20(1).pdf YouTube Yahoo Q Amazon ® D2L ix Achievements Mylaurentian Email 2 (Pilestats.java) 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:luseralaaronDesktop)>java rilestats Enter input file nane: textl.txt 32 1ines 159 words 810 characters. Once you have that working, make the following changes. Collect the fillename 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 Caluserslaaron Desktopl>java Filestats Enter input file nane: textl.txt Enter a string to search for: and 33 1ines 159 words 810 characters Your requested word and was found 8 times 6: And what to do and bow to be 10: Sut there in the sandpile at Sunday achool. 21: Wash your hands befoze you eat 23: tar cookies and cold milk are good for you. 25: Learn some and think some 26: And dzaw and paint and sing and dance 27: And play and work everyday sone. 1: Nold hands and atick together.

Explanation / Answer

// I hope this will help you. use - javac FileChooserExample.java

// & then run it using - java FileChooserExample

import javax.swing.*;
import java.io.*;
import java.io.File;
import java.util.Scanner;

public class FileChooserExample extends JFrame{
public static void main(String[] args) {
JFileChooser chooser = new JFileChooser();
File selectedFile = null;
int retval = JFileChooser.CANCEL_OPTION;
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
retval = chooser.showDialog(null, "Select");
String absolutePath = "";
if (retval == chooser.APPROVE_OPTION) {
selectedFile = chooser.getSelectedFile();
absolutePath = selectedFile.getAbsolutePath();
}
try{
Scanner kb = new Scanner(System.in);
Scanner input = new Scanner(selectedFile);

System.out.print("Enter a file: ");
System.out.println(selectedFile);
System.out.print("Enter a string to search for: ");
String key = kb.next();
  
int keyfound=0;
int chars = 0;
int lineCount = 0;
while(input.hasNextLine()) {
lineCount++;
String next = input.nextLine();
for(int i=0;i<next.length();i++)
{
if(next.charAt(i)!=' ' && next.charAt(i)!=' ')
chars ++;
}
if (next.equalsIgnoreCase(key)){
keyfound++;
}
}
System.out.println(lineCount+" lines");

int words = 0;
input = new Scanner(selectedFile);
while(input.hasNext()){
words++;
input.next();
}
System.out.println(words+" words");

System.out.println(chars+" characters");

System.out.println();
input = new Scanner(selectedFile);
while(input.hasNext()) {
String next = input.next();
if (next.equalsIgnoreCase(key) && next !=null){
keyfound++;
}
}
System.out.println("You requested word -"+key+"- was found "+keyfound+" times");
  
int lineNo = 0,index =0;
input = new Scanner(selectedFile);
String line = "";
while(input.hasNextLine() && input!=null){
while(input.hasNext()) {
String next = input.next();
if (next.equalsIgnoreCase(key) && next!=null){
index = lineNo + 1;
System.out.println(index+": "+input.nextLine());
}
}
}
}catch (Exception ex) {ex.printStackTrace(); }
}
}  

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