Question

Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

Trying to practice this assignment

Argument list: the *yahoonews.txt Data file: yahoonews.txt

Write a program named WordCount.java, in this program, implement two static methods as specified below:
   public static int countWord(Sting word, String str)
   this method counts the number of occurrence of the word in the String (str)

   public static int countWord(String word, File file)
  This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file include , ! . ; : ? & # and $.

Then test your method with the data file.

This is my code so far and I could use lots of help and explanations to make this work

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class WordCount{

   public static void main(String[] args){
      File file = new File(args[0]);
      System.out.println("File name is: "+file);
      String word = "";
             
    
    
    
   }
   public static int countWord(String word, File file){
      int counter =0;
      try{
         BufferedReader b = new BufferedReader(new FileReader(file));
         while((readLine=b.readLine())!=null){
            for(String s: readLine.split(" ")){
               if(s.toLowerCase()==word.toLowerCase()){
                  count++;
             
               }
            }
         }
      
      }catch(IOException e){
         System.out.println("errors");
      }
      System.out.println("count is: " +count);
      return count;


   }


   public static int countWord(String word, String str){
      int count =0;
      Scanner sc = new Scanner(str.toLowerCase());
      word=word.toLowerCase();
      for (int i = 0; i < str.length(); i++) {
         while(sc.hasNext()){
            String temp = sc.hasNext();
            if(temp=="word") count++;
            System.out.println("Current or next line: "+sc.hasNext());
         }
    
      }

      return count;

   }

}

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

I would like you to modify code as follows:

Instead of taking list of arguments take just filename as argument.

If you need to find the word count from file then the function would be called accordingly.

To get this calculate the no.of arguments.

Also you can take the word whose count is to be found through the argument.

If arguments length is 2 then consider filename is also included otherwise only word is given as argument

Also you need to read the string and pass it as argument to the function but not read it from within the function.

//java code

mport java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class WordCount{

   public static void main(String[] args){

int no_arg = args.length();

if(length==2){
      File file = new File(args[0]);
      System.out.println("File name is: "+file);


      String word = args[1];

System.out.println("The word count is" + countWord(word,file));

}

else

{

String word=args[0];

//read string

StringBuilder everything = new StringBuilder();
    String line;
BufferedReader buffIn = new BufferedReader(System.in);
    while( (line = buffIn.readLine()) != null) {
       everything.append(line);
    }

String str=new String(everything);

System.out.println("The word count is " + wordCount(word,str));
}


             
    
    
    
   }
   public static int countWord(String word, File file){
      int counter =0;
      try{
         BufferedReader b = new BufferedReader(new FileReader(file));
         while((readLine=b.readLine())!=null){
            for(String s: readLine.split(" ")){
               if(s.toLowerCase()==word.toLowerCase()){
                  count++;
             
               }
            }
         }
      
      }catch(IOException e){
         System.out.println("errors");
      }
      System.out.println("count is: " +count);
      return count;


   }


   public static int countWord(String word, String str){
      int count =0;

Scanner sc = new Scanner(str.toLowerCase());
      word=word.toLowerCase();
      for (int i = 0; i < str.length(); i++) {
         while(sc.hasNext()){
            String temp = sc.hasNext();
            if(temp=="word") count++;
            System.out.println("Current or next line: "+sc.hasNext());



    
      }

      return count;

   }

}

As we are taking input as multiple lines we can use simple word count function for string

But you need to replace all \n with " "(space)

static int countOccurences(String str, String word)

{

    // split the string by spaces in a

    String a[] = str.split(" ");

  

    // search for pattern in a

    int count = 0;

    for (int i = 0; i < a.length; i++)

    {

    // if match found increase count

    if (word.equals(a[i]))

        count++;

    }

  

    return count;

}

Add a comment
Know the answer?
Add Answer to:
Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...
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 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); } //...

  • Java Project Draw a class diagram for the below class (using UML notation) import java.io.BufferedReader; import...

    Java Project Draw a class diagram for the below class (using UML notation) import java.io.BufferedReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; public class myData { public static void main(String[] args) { String str; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter text (‘stop’ to quit)."); try (FileWriter fw = new FileWriter("test.txt")) { do { System.out.print(": "); str = br.readLine(); if (str.compareTo("stop") == 0) break; str = str + "\r\n"; // add newline fw.write(str); } while (str.compareTo("stop") != 0); } catch (IOException...

  • Assignment 3: Word Frequencies Prepare a text file that contains text to analyze. It could be...

    Assignment 3: Word Frequencies Prepare a text file that contains text to analyze. It could be song lyrics to your favorite song. With your code, you’ll read from the text file and capture the data into a data structure. Using a data structure, write the code to count the appearance of each unique word in the lyrics. Print out a word frequency list. Example of the word frequency list: 100: frog 94: dog 43: cog 20: bog Advice: You can...

  • 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...

  • I need help understanding this programming assignment. I do not understand it at all. Someone provided...

    I need help understanding this programming assignment. I do not understand it at all. Someone provided me with the code but when I run the code on eclipse it gives an error. Please explain this assignment to me please. Here is the code: import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class TextEditor { public static List<String> lines = new ArrayList<String>(); public static void main(String[] args) throws IOException { Scanner s = new...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • 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 help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

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
ADVERTISEMENT