Question

In Java Programming chapter 6 Make a class that represents a file. This class will have...

In Java Programming chapter 6

Make a class that represents a file. This class will have the ability to calculate the number of lines in that file and the ability to search through the file.

UML diagram:

-filename:String

+FileStats(String fname)

+getNumMatchingWords(String wordtoFind) : int

+getNumLines() : int

The getNumMatchingWords method will take a bit of text and determine how many lines contain that text. Make the comparison not care about case.

View required output

Test Case 1

Files in the same directory
  • bill-of-rights.txt
  • dictionary.txt
  • romeo-and-juliet.txt
  • FileStatsDemo.java
Num lines in Bill of Rights: 65\n
Num lines in Romeo and Juliet: 5268\n
Num lines in dictionary: 21882\n
-- Bill of Rights Test --\n
'the' count: 29\n
'no' count: 17\n
'rights' count: 2\n
-- Romeo and Juliet Test --\n
'Romeo' count: 304\n
'Juliet' count: 185\n
'end' count: 95\n
'emoji' count: 0\n

here how the demo looks like

import java.io.FileNotFoundException;

public class FileStatsDemo
{
    public static void main(String[] args) throws FileNotFoundException {

        FileStats billOfRights = new FileStats("bill-of-rights.txt");
        FileStats romeoAndJuliet = new FileStats("romeo-and-juliet.txt");
        FileStats dictionary = new FileStats("dictionary.txt");

        System.out.println("Num lines in Bill of Rights: " + billOfRights.getNumLines());
        System.out.println("Num lines in Romeo and Juliet: " + romeoAndJuliet.getNumLines());
        System.out.println("Num lines in dictionary: " + dictionary.getNumLines());

        System.out.println("-- Bill of Rights Test --");
        System.out.println("'the' count: " + billOfRights.getNumMatchingWords("the"));
        System.out.println("'no' count: " + billOfRights.getNumMatchingWords("no"));
        System.out.println("'rights' count: " + billOfRights.getNumMatchingWords("rights"));

        System.out.println("-- Romeo and Juliet Test --");
        System.out.println("'Romeo' count: " + romeoAndJuliet.getNumMatchingWords("ROMEO"));
        System.out.println("'Juliet' count: " + romeoAndJuliet.getNumMatchingWords("juliet"));
        System.out.println("'end' count: " + romeoAndJuliet.getNumMatchingWords("end"));
        System.out.println("'emoji' count: " + romeoAndJuliet.getNumMatchingWords("emoji"));

    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class FileStartsDemo {
public String fileName;

public FileStartsDemo(String fileName) {
   this.fileName = fileName;
}

   public int getNumMatchingWords(String inputSearch) {
      
       double countBuffer=0;
       int lineNumber=0;
       BufferedReader br;
       String line = "";
try {
   br = new BufferedReader(new FileReader(fileName));

while((line = br.readLine()) != null){

String[] words = line.split(" ");

for (String word : words) {
if (word.equals(inputSearch)) {
countBuffer++;
}
}

if(countBuffer > 0) {
countBuffer = 0;
lineNumber++;
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return lineNumber;
}
  
   public int getNumLines() {
      
   int lineNumber = 0;

       try{
           File file=new File(fileName);
       if(file.exists()){
          
       FileReader fr = new FileReader(file);
       LineNumberReader lnr = new LineNumberReader(fr);
      
      
   while (lnr.readLine() != null){
       lineNumber++;
   }
   lnr.close();
  
       }else{
           System.out.println("File does not exists!");
       }
      
   }catch(IOException e){
       e.printStackTrace();
   }
       return lineNumber;
   }

   public static void main(String[] args) throws FileNotFoundException{
      
       FileStartsDemo c = new FileStartsDemo("C:\\Users\\durga.chityala\\Desktop\\abc.txt");
  
       System.out.println("-----abc-----");
       System.out.println("number of lines in abc: "+c.getNumLines());
       System.out.println("java count: "+c.getNumMatchingWords("java"));

   }
}

Add a comment
Know the answer?
Add Answer to:
In Java Programming chapter 6 Make a class that represents a file. This class will have...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Ask the user for the name of a file and a word. Using the FileStats class,...

    Ask the user for the name of a file and a word. Using the FileStats class, show how many lines the file has and how many lines contain the text. Standard Input                 Files in the same directory romeo-and-juliet.txt the romeo-and-juliet.txt Required Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 1137 line(s) contain "the"\n Your Program's Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 553 line(s) contain "the"\n (Your output is too short.) My...

  • I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...

    I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Activity 2. Complete the code inside the Java file below, ListUtil.java, such that this class supplies...

    Activity 2. Complete the code inside the Java file below, ListUtil.java, such that this class supplies a utility method to reverse the entries in a linked list. Then, test your code using the tester class, Reverse Tester.java, given below. ListUtil.java import java.util.LinkedList; /** This class supplies a utility method to reverse the entries in a linked list. */ public class ListUtil { /** Reverses the elements in a linked list @param strings the linked list to reverse public static void...

  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

  • How can I make this program sort strings that are in a text file and not...

    How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please. import java.util.*; public class MergeDemo { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("How many lines to be sorted:"); int size=input.nextInt(); String[] lines=new String[size]; lines[0]=input.nextLine(); System.out.println("please enter lines..."); for(int i=0;i { lines[i]=input.nextLine(); } System.out.println(); System.out.println("Lines Before Sorting:"); System.out.println(Arrays.toString(lines)); mergeSort(lines); System.out.println(); System.out.println("Lines after Sorting:"); System.out.println(Arrays.toString(lines)); } public...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year...

    JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year of storm/ Name of storm/ mmdd storm started/ mmdd storm ended/ magnitude of storm/ //made up data 2004/Ali/1212/1219/1 2003/Bob/1123/1222/3 1980/Sarah/0123/0312/0 1956/Michael/1211/1223/4 1988/Ryan/0926/1019/ 1976/Tim/0318/1010/0 2006/Ronald/0919/1012/2 1996/Mona/0707/0723/1 2000/Kim/0101/0201/1 2001/Jim/1101/1201/3 Text file Class storm{ private String nameStorm; private int yearStorm; private int startStorm; private int endStorm; private int magStorm; public storm(String name, int year, int start, int end, int mag){ nameStorm = name; yearStorm = year; startStorm...

  • Use java programming for the following questions to have the solutions ready within today Jan 3...

    Use java programming for the following questions to have the solutions ready within today Jan 3 2020 at or before 8:00PM GMT+8 time zone: Question 5 A method secret() of a class F is shown below: public int secret(String[] stringArray, int n) { int num = 0; for (int i=0; i<stringArray.length; i++) if (stringArray[i].indexOf("a", n) >= 0) num++; return num; } A program segment using the method is: F f = new F(); String[] array01 = {"an", "easy", "exam", "on",...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT