Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hi! So I'm trying to figure this question out, but I'm not sure what to write for the code. Thanks. (this is in java) Now
Hi! So I'm trying to figure this question out, but I'm not sure what to write for the code. Thanks. (this is in java)
Now its time to write the code that will replace a given placeholder with a given replacement String. Writing this method will make it much easier to go through and replace every placeholder in the template.
Write the following method:
/** * Replaces the first instance of the String placeholder * inside the String template with the String replacement. * Returns a new String that is the result of this replacement. * If the placeholder does not exist in the template, * then the template passed in is returned, no replacement * is made. */ private String replacePlaceholder(String template, String placeholder, String replacement)
For example, the following code:
String template = "The [Adjective] [Noun] jumped over the [Adjective] [Noun]."; String replaced = replacePlaceholder(template, "[Adjective]", "silly"); System.out.println(template); System.out.println(replaced);
Should print out:
The [Adjective] [Noun] jumped over the [Adjective] [Noun]. The silly [Noun] jumped over the [Adjective] [Noun].
Mad Libs
M la !!!! FILES 1 import java.io.*; 2 import java.nio.charset. Standardcharsets; 3 import java.nio.file.Files; 4 import java.nio.file.Paths; 5 6 // This is the MadLibFileReader class 7 // It reads in a madlib text file and converts it into a Java String 8 // Feel free to explore this code, it uses the Java Files class 9 // to read in a Mad Lib template from the "madlib.txt" file. // DO NOT CHANGE THIS CODE, the program may not work if you do. public class MadLibFileReader 12 - { private static final String FILENAME "madlib.txt"; MadLibFileReader.java madlib.txt D MadLib.java 10 11 13 14 15 private String template; public MadLibFileReader() { template = loadTemplate(FILENAME); } 16 17 18- 19 20 21 22 23- 24 25 26 27 public String getMadLibTemplate() { return template; }Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started