Write a program that reads from the original script from Star War (A New Hope) a
ID: 3761775 • Letter: W
Question
Write a program that reads from the original script from Star War (A New Hope) and finds the number of times the words, “force”, “wookiee”, “jabba”, and “death star” are used.
script file: StarWarNewHopeScript.txt
First download the script
It ignores case and punctuation
Wookiee would count
Wookiee. Would count
Wookiee! Would count
Wookiee? Would count
Wookiee, would count
WOOKIE, would count
Write your own driver!
Example Dialog:
Scanning Star Wars
The number of times the word "Force" appears in Star Wars is 30
The number of times the word "Wookiee" appears in Star Wars is 19
The number of times the word "Jabba" appears in Star Wars is 16
The number of times the word "Death Star" appears in Star Wars is 224
Explanation / Answer
Answer:
import java.util.Scanner;
import java.io.*;
public class SpecificWordCounter
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the name of the text file:");
String name = keyboard.nextLine();
File file = new File(name);
System.out.println("Enter the word you are searching for in the text file:");
String word = keyboard.nextLine();
try
{
System.out.println("The word ""+word+"" appeared "+ searchCount(file,word) + "
times in the file "+ file);
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
public static int searchCount(File fileA, String fileWord) throws FileNotFoundException
{
int count = 0;
fileWord = fileWord.trim();
Scanner scanner = new Scanner(fileA);
while (scanner.hasNext())
{
String nextWord = scanner.next().trim();
if (nextWord.equals(fileWord)) {
++count;
}
}
return count;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.