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
  • 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); } //...

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

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

  • I need help with my code when I run my code running the wrong thing like...

    I need help with my code when I run my code running the wrong thing like this After downSize() words.length=60003 wordCount=60003 vowelCount=206728 this is my code here import java.io.*; import java.util.*; public class Project02 {    static final int INITIAL_CAPACITY = 10;    public static void main (String[] args) throws Exception    {        // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE        if (args.length < 1 )       ...

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

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

  • JAVA: Rewrite the following code to where the inputs are from a file. The file name...

    JAVA: Rewrite the following code to where the inputs are from a file. The file name will be from the input from the user scanner. CODE: import java.util.*; public class Q1 { public static int sumOD (int k) { int sumOD = 0; int in = k; while (in != 0) { int digitON = in % 10; sumOD += digitON; in /= 10; } return sumOD; }    public static void main(String[] args) { int n, i, k; System.out.println("Enter...

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

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

  • Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){...

    Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter a word:"); String word = kb.next();    String reverse = ""; for (int i=word.length()-1; i>=0; i--) reverse += word.charAt(i); boolean result = reverse.equalsIgnoreCase(word);    if (result) System.out.println("The word " +word+ " is a Palindrome."); else System.out.println("The word " +word+ " is not a Palindrome."); } } Rewrite the program so that the main method is: public static void...

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