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

Lab 20 DANGER ZONE Objective: Write a program that reads the lyrics to the song

ID: 3763124 • Letter: L

Question

Lab 20

DANGER ZONE

Objective:

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

Finally:

Upload file to the dropbox

Explanation / Answer

/**The java program prompts user to enter a word to replace
* a song lyric word danger with user input word.
* The new text file is created and write to the file.*/
//DanzerZone.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class DanzerZone
{
   public static void main(String[] args)
   {
  
       //create Scanner class
       Scanner scanner=new Scanner(System.in);
      
       //Declare scanner and printwriter class
       Scanner file=null;
       PrintWriter writer=null;
      
       //declare string variable
       String line="";
       String replace="";
       String outputLine="";
      
      
      
       try
       {
          
           //open input file lyrics.txt
           file=new Scanner(new File("lyrics.txt"));
           //user delimiter for new line
           file.useDelimiter(" ");
           //open output file object for newSong.txt file
           writer=new PrintWriter(new File("newSong.txt"));
          
          
          
           System.out.println("Enter a word to replace DANGER with ");
           //prompt for replace word
           replace=scanner.nextLine();
          
           while(file.hasNextLine())
           {
               //read line from input file
               line=file.nextLine();
               //use split method on line
               String[] words=line.split(" ");
              
               int count=0;
               while(count<words.length)
               {
                   //check if the word is matched with replace
                   if(words[count].equalsIgnoreCase(replace))
                       //then concatenate with string outputLine
                       outputLine+=replace+" ";
                   else
                       //otherwise concatenate word
                       outputLine+=words[count]+" ";
                  
                   //increment count by one
                   count++;
               }
              
               //write outputLine to the output file
               writer.println(outputLine);
               //reset outputLine to empty
               outputLine="";          
           }          
           System.out.println("Finished writing to newSong.txt");          
       }
       catch (FileNotFoundException e)
       {
           System.out.println(e);
       }
      
      
       //close file streams
       scanner.close();
       file.close();
       writer.close();
      
      
   }
}


-----------------------

output lyrics.txt text file

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 file

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