Write a program that reads the lyrics to the song “Danger Zone” by Kenny Loggins
ID: 3763224 • Letter: W
Question
Write a program that reads the lyrics to the song “Danger Zone” by Kenny Loggins and replaces the word, “danger” with something else. The system should ask the user for a word to replace “danger”. The program should replace every instance ignoring capitalization, plurals, punctuations, etc. Also at the end the program should print out the new song to a new file.
First download the lyrics and place them in your project’s directory
The system should blindly replace any instance of photograph including plurals, capitalizations, or punctuations.
Danger counts
danger counts
dangers counts
danger! Counts
And so on
The word to replace “danger” can simply replace the word without any additional modifications. In other words, don’t worry about the pluralizing or adding punctuation to the new word.
Make sure to print the new lyrics to a new file.
It doesn’t have to match the formatting of the original.
Write your own main method!
Hints and Tips:
I would not advise anyone use the string method .replace unless they know how to use regular expressions confidently.
The scanner method .useDelimiter can be used to separate each of the words delimited by white space (“ “), while also ignoring end line’s (“ ”) and carriage returns (“ ”).
Example Dialog:
Enter a word to replace DANGER with
taco
Finished writing to newSong.txt
Example File Print out:
Revvin' up your engine
Listen to her howlin' roar
Metal under tension
Beggin' you to touch and go
Highway to the taco Zone
Ride into the taco Zone
Headin' into twilight
Spreadin' out her wings tonight
She got you jumpin' off the deck
And shovin' into overdrive
Highway to the taco Zone
I'll take you right into the taco Zone
You'll never say hello to you
Until you get it on the red line overload
You'll never know what you can do
Until you get it up as high as you can go
Out along the edges
Always where I burn to be
The further on the edge
The hotter the intensity
Highway to the taco Zone
Gonna take it right into the taco Zone
Highway to the taco Zone
Ride into the taco Zone
Highway to the taco Zone
Gonna take it right into the taco Zone
Highway to the taco Zone
Ride into the taco Zone
Highway to the taco Zone
Gonna take it right into the taco Zone
Highway to the taco Zone
Ride into the taco Zone
Explanation / Answer
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
public class Replace {
public static void main(String args[]) throws Exception {
String str;
try {
FileInputStream fis2 = new FileInputStream("lirics.txt");
DataInputStream input = new DataInputStream(fis2);
FileOutputStream fos2 = new FileOutputStream("newSong.txt");
DataOutputStream output = new DataOutputStream(fos2);
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a word to replace DANGER with ");
String s2 = "DANGER";
String s3 = scanner.nextLine();
while (null != ((str = input.readLine()))) {
int x = 0;
int y = 0;
String result = "";
while ((x = str.indexOf(s2, y)) > -1) {
result += str.substring(y, x);
result += s3;
y = x + s2.length();
}
result += str.substring(y);
str = result;
if (str.indexOf("'',") != -1) {
continue;
} else {
str = str + " ";
output.writeBytes(str);
}
}
System.out.println("Finished writing to newSong.txt");
} catch (Exception ioe) {
System.err.println("I/O Error - " + ioe);
}
}
}
lyrics.txt:
Revvin' up your engine
Listen to her howlin' roar
Metal under tension
Beggin' you to touch and go
Highway to the DANGER Zone
Ride into the DANGER Zone
Headin' into twilight
Spreadin' out her wings tonight
She got you jumpin' off the deck
And shovin' into overdrive
Highway to the DANGER Zone
I'll take you right into the DANGER Zone
You'll never say hello to you
Until you get it on the red line overload
You'll never know what you can do
Until you get it up as high as you can go
Out along the edges
Always where I burn to be
The further on the edge
The hotter the intensity
Highway to the DANGER Zone
Gonna take it right into the DANGER Zone
Highway to the DANGER Zone
Ride into the DANGER Zone
Highway to the DANGER Zone
Gonna take it right into the DANGER Zone
Highway to the DANGER Zone
Ride into the DANGER Zone
Highway to the DANGER Zone
Gonna take it right into the DANGER Zone
Highway to the DANGER Zone
Ride into the DANGER Zone
newSong.txt:
Revvin' up your engine
Listen to her howlin' roar
Metal under tension
Beggin' you to touch and go
Highway to the toco Zone
Ride into the toco Zone
Headin' into twilight
Spreadin' out her wings tonight
She got you jumpin' off the deck
And shovin' into overdrive
Highway to the toco Zone
I'll take you right into the toco Zone
You'll never say hello to you
Until you get it on the red line overload
You'll never know what you can do
Until you get it up as high as you can go
Out along the edges
Always where I burn to be
The further on the edge
The hotter the intensity
Highway to the toco Zone
Gonna take it right into the toco Zone
Highway to the toco Zone
Ride into the toco Zone
Highway to the toco Zone
Gonna take it right into the toco Zone
Highway to the toco Zone
Ride into the toco Zone
Highway to the toco Zone
Gonna take it right into the toco Zone
Highway to the toco Zone
Ride into the toco Zone
OUTPUT:
Enter a word to replace DANGER with
toco
Finished writing to newSong.txt
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.