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

Word Count Write a program that asks the user to enter a word. The program then

ID: 3774485 • Letter: W

Question

Word Count

Write a program that asks the user to enter a word. The program then reads ‘The Raven’ by Edgar Allen Poe from a text file, and returns the number of times that word appears.

You will need:

A text file with the poem in it. (A copy of the file has been uploaded to Canvas.) Make sure to download it to an appropriate location on your computer!

Two Scanner objects – one to read the user’s word from the keyboard, and another to read the text file.

A while loop to read the poem.

The loop will use the hasNext method to ensure that the contents of the files are read.

Use the next method instead of the nextLine method to read word by word.

You will need a nested if statement – this will be used to compare the user’s word to every word in the poem.

Code Shell

//*********************************************************
// WordCount.java
//
// This program asks the user for a word and checks to see
// the number of times it appears in 'The Raven'.
//*********************************************************
import java.util.Scanner; // Needed for the Scanner class
import java.io.*;         // Needed for the File class

public class WordCount
{
   public static void main(String[] args) throws IOException
   {
     int wcount = 0; // keeps track of word
     String word, entry;
    
     System.out.println("This code lets a user enter a word "
         + " and checks 'The Raven' to see how many times "
         + "it appears.");
    
     // Open the file.

     // Read from the keyboard too!
    
     // Ask the user for a word and store it in entry

     // Read lines from the file until no more are left.
     while ()
     {
         // Use the word variable to read the file
         if () // if word is equal to entry
     }

     // Close the file.

     // Print the number of times the word appears    
   }
}

Sample Output 1

This code lets a user enter a word and checks 'The Raven' to see how many times it appears.
Enter a word to look for: Lenore
The word Lenore appears 8 times.

Sample Output 2

This code lets a user enter a word and checks 'The Raven' to see how many times it appears.
Enter a word to look for: nevermore
The word nevermore appears 11 times.

Explanation / Answer

WordCount.java

import java.util.Scanner; // Needed for the Scanner class
import java.io.*; // Needed for the File class

public class WordCount
{
public static void main(String[] args) throws IOException
{
int wcount = 0; // keeps track of word
String word, entry;
  
System.out.println("This code lets a user enter a word "
+ " and checks 'The Raven' to see how many times "
+ "it appears.");
  
// Open the file.
File file = new File("D:\input.txt");

// Read from the keyboard too!
Scanner scan = new Scanner(System.in);
Scanner scan1 = new Scanner(file);

// Ask the user for a word and store it in entry
   System.out.print("Enter a word to look for: ");
   entry = scan.next();

// Read lines from the file until no more are left.
while (scan1.hasNext())
{
// Use the word variable to read the file
   word = scan1.next();
if (word.equalsIgnoreCase(entry)){ // if word is equal to entry
   wcount++;
}

}

// Close the file.
        scan.close();
        scan1.close();

// Print the number of times the word appears
        System.out.println("The word nevermore appears "+wcount+" times.");
}
}


Output:

This code lets a user enter a word and checks 'The Raven' to see how many times it appears.
Enter a word to look for: Mary
The word nevermore appears 2 times.

input.txt

Mary had a little lamb
Its fleece was white as snow
And everywhere that Mary went
The lamb was sure to go.

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