Write a program that reads from the original script from Star War (A New Hope) a
ID: 3761723 • 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. This needs to be done in Java
First download the script here: http://bit.ly/1kp9dRp
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
import java.io.File;
import java.io.FileNotFoundException;
import static java.lang.System.in;
import java.util.Scanner;
public class Starwars {
public static void main(String[] args) throws FileNotFoundException {
//Replace the path of the file in your computer below.
File file = new File("C:\Users\Rajeev\Desktop\chegg\StarWarsNewHopeScript.txt");
Scanner in = new Scanner(file);
String word;
int hat=0;
int count1=0,count2=0,count3=0,count4=0;
while (in.hasNext()) {
word = in.next();
word = word.replaceAll("[^a-zA-Z ]", "").toLowerCase();
if(word.contains("force"))
{
count1++;
hat=0;
//System.out.println(word);
}
else if(word.contains("wookiee"))
{
count2++;
hat=0;
//System.out.println(word);
}
else if(word.contains("jabba"))
{
count3++;
hat =0;
//System.out.println(word);
}
else if(word.contains("death"))
{
hat = 1;
//System.out.println(word);
}
else if(word.contains("star") && hat==1)
{
count4++;
hat=0;
//System.out.println(word);
}
else
{
hat=0;
}
}
System.out.println(count1);
System.out.println(count2);
System.out.println(count3);
System.out.println(count4);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.