7.11 Lab: Decoder part 3 - text expander (String search/replacement) This progra
ID: 3703666 • Letter: 7
Question
7.11 Lab: Decoder part 3 - text expander (String search/replacement)
This program will find known text message abbreviations within a string and replace them with their actual meanings in unabbreviated form.
(1) Implement for Test 1:
Use nextLine() with a Scanner object variable to read one line of user input into a String variable.
Output that line.
Example:
(2) Implement for remaining tests: (Test 5 is a blind test)
Expand the common text message abbreviations.
Output a message for each abbreviation that is expanded,
and use replace() on the input String to expand the abbreviation.
After all replacements have been made, output the expanded line.
NOTE: An abbreviation may appear more than once in the text,
but you will only need to write code to search and replace once per abbreviation.
Support these abbreviations (searching for them, in the order listed):
BFF -- best friend forever
IDK -- I don't know
JK -- just kidding
TTYL -- talk to you later
Example:
import java.util.Scanner;
public class TextMsgExpander {
public static void main(String[] args) {
/* Type your code here. */
return;
}
}
Explanation / Answer
TextMsgExpander.java
import java.util.Scanner;
public class TextMsgExpander {
public static void main(String[] args) {
new TextMsgExpander().run();
}
public void run()
{
System.out.print("Enter text:");
Scanner scnr = new Scanner(System.in);
String inString = scnr.nextLine();
System.out.print("You entered:");
System.out.println(inString);
String message = newMessage(inString);
System.out.println();
if(inString.contains("IDK"))
System.out.println("Replaced "IDK" with "I don't know".");
if(inString.contains("BFF"))
System.out.println("Replaced "BFF" with "best friend forever".");
if(inString.contains("JK"))
System.out.println("Replaced "JK" with "just kidding".");
if(inString.contains("TMI"))
System.out.println("Replaced "TMI" with "too much information".");
if(inString.contains("TTYL"))
System.out.println("Replaced "TTYL" with "talk to you later".");
System.out.println();
System.out.println("Expanded: "+message);
}
public String newMessage(String str)
{
String Str = str;
Str = Str.replace("BFF", "best friend forever");
Str = Str.replace("IDK", "I don't know");
Str = Str.replace("JK", "just kidding");
Str = Str.replace("TMI", "too much information");
Str = Str.replace("TTYL", "talk to you later");
return Str;
}
}
Output:
Enter text:IDK how that happened. TTYL.
You entered:IDK how that happened. TTYL.
Replaced "IDK" with "I don't know".
Replaced "TTYL" with "talk to you later".
Expanded: I don't know how that happened. talk to you later.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.