One of the most characters in fiction is named "Tarzan of the ." Tarzan was raised by a/an and lives in the jungle in the heart of darkest . He spends most of his time eating and swinging from tree to Whenever he gets angry, he beats on his chest and says, " !" This is his war cry. Tarzan always dresses in shorts made from the skin of a/an and his best friend is a/an chimpanzee named Cheetah. He is supposed to be able to speak to elephants and In the movies, Tarzan is played by
-
At least 8 lines
-
At least 5 placeholders
- below is how to format the madlib.
package string;
import java.io.*;
import java.util.*;
// Class FindReplace definition
public class FindReplace
{
// To store the string read from file
String original[];
// To store number of lines
int rows = 0;
// Method to read file contents and stores it in string array
void fileRead()
{
// To handle file not found
try
{
// Scanner class object created to read file
Scanner readF = new Scanner(new File("FindReplaceData.txt"));
// Loops till end of the file to count number of lines
while(readF.hasNextLine())
{
// Read one line at a time from file
readF.nextLine();
// Increase the row counter
rows++;
}// End of while loop
// Allocates number of rows
original = new String[rows];
// Close the file
readF.close();
// Re opens the file for reading data
readF = new Scanner(new File("FindReplaceData.txt"));
// Reset the row counter to zero
rows = 0;
// Loops till end of the file to read data
while(readF.hasNextLine())
{
// Read one line at a time from file and stores at rows index position of original array
original[rows] = readF.nextLine();
// Increase the row counter by one
rows++;
}// End of while loop
System.out.print("\n Original Data \n");
// loops till number of rows and displays the original data
for(int y = 0; y < rows; y++)
System.out.println(original[y]);
// Close the file
readF.close();
}// End of try block
// Catch block to handle FileNotFoundException
catch(FileNotFoundException fe)
{
System.out.println("\n File not found");
}// End of catch block
}// End of method
// Method to find and replace
void findReplace()
{
// Scanner class object created to accept data from console
Scanner sc = new Scanner(System.in);
// Creates find array
String find[] = {"<adjective>", "<plural-noun>", "<noun>", "<adjective>", "<place>", "<plural-noun>", "<noun>", "<funny-noise> !",
"<adjective>", "<noun>", "<adjective>", "<plural-noun>", "<person's-name>"};
// Creates an array to store replace with string entered by the user
String replaceWith[] = new String[find.length];
// Accepts the string to replace from the user and replaces it
System.out.print("\n Please type a adjective: ");
replaceWith[0] = " " + sc.next() + " ";
original[0] = original[0].replaceAll(find[0], replaceWith[0]);
System.out.print("\n Please type a plural-noun: ");
replaceWith[1] = " " + sc.next() + " ";
original[1] = original[1].replaceAll(find[1], replaceWith[1]);
System.out.print("\n Please type a noun: ");
replaceWith[2] = " " + sc.next() + " ";
original[2] = original[2].replaceAll(find[2], replaceWith[2]);
System.out.print("\n Please type a adjective: ");
replaceWith[3] = " " + sc.next() + " ";
original[2] = original[2].replaceAll(find[3], replaceWith[3]);
System.out.print("\n Please type a place: ");
replaceWith[4] = " " + sc.next() + " ";
original[3] = original[3].replaceAll(find[4], replaceWith[4]);
System.out.print("\n Please type a plural-noun: ");
replaceWith[5] = " " + sc.next() + " ";
System.out.print("\n Please type a noun: ");
replaceWith[6] = " " + sc.next() + " ";
original[4] = original[4].replaceAll(find[5], replaceWith[5]);
original[4] = original[4].replaceAll(find[6], replaceWith[6]);
System.out.print("\n Please type a funny-noise: ");
replaceWith[7] = " " + sc.next() + " ";
original[6] = original[6].replaceAll(find[7], replaceWith[7]);
System.out.print("\n Please type a adjective: ");
replaceWith[8] = " " + sc.next() + " ";
System.out.print("\n Please type a noun: ");
replaceWith[9] = " " + sc.next() + " ";
original[7] = original[7].replaceAll(find[8], replaceWith[8]);
original[7] = original[7].replaceAll(find[9], replaceWith[9]);
System.out.print("\n Please type a adjective: ");
replaceWith[10] = " " + sc.next() + " ";
original[8] = original[8].replaceAll(find[10], replaceWith[10]);
System.out.print("\n Please type a plural-noun: ");
replaceWith[11] = " " + sc.next() + " ";
original[10] = original[10].replaceAll(find[11], replaceWith[11]);
System.out.print("\n Please type a person's-name: ");
replaceWith[12] = " " + sc.next() + " ";
original[10] = original[10].replaceAll(find[12], replaceWith[12]);
// Close the scanner
sc.close();
}// End of method
// Method to display replaced data
void display()
{
System.out.print("\n Replaced Data \n");
// loops till number of rows and displays the replaced data
for(int y = 0; y < rows; y++)
System.out.println(original[y]);
}// End of while loop
// main method definition
public static void main(String args[])
{
// Creates an object of class FindReplace
FindReplace fr = new FindReplace();
// Call the method to read file contents
fr.fileRead();
// Calls the method to find and replace
fr.findReplace();
// Calls the method to display replaced data
fr.display();
}// End of main method
}// End of class
Step by step
Solved in 2 steps