JAVA (1) Use scnr.nextLine(); to get a line of user input into a string. Output
ID: 3585350 • Letter: J
Question
JAVA
(1) Use scnr.nextLine(); to get a line of user input into a string. Output that line.
Ex:
(2) Again, copy the method you wrote in Part 1 into the class of Part 3.
Expand common text message abbreviations. Output a message for each abbreviation that is expanded, then output the expanded line. Be sure to use your decoding method from Part 1 to decoded the abbreviations.
Note: for the tests to pass, you need to handle the abbreviations in the exact order that they are listed below.
Support these abbreviations:
LOL -- laughing out loud
IDK -- I don't know
BFF -- best friends forever
IMHO -- in my humble opinion
TMI -- too much information
Ex:
Here is part one added to the code:
import java.util.Scanner;
public class TextMsgExpander {
/* Add your method from Part 1 here */
public static String decTextAbbr(String textAbbr) {
String IDK = "I don't know";
String TMI = "too much information";
String BFF = "best friends forever";
String IMHO = "in my humble opinion";
String LOL = "laughing out loud";
if(textAbbr.equals("LOL")) {
return LOL;
}
else if(textAbbr.equals("IDK")) {
return IDK;
}
else if(textAbbr.equals("BFF")) {
return BFF;
}
else if(textAbbr.equals("IMHO")) {
return IMHO;
}
else if(textAbbr.equals("TMI")) {
return TMI;
}
else {
return "Unknown";
}
}
public static void main(String[] args) {
/* Type your code here. */
return;
}
}
Explanation / Answer
TextMsgExpander.java
import java.util.Scanner;
public class TextMsgExpander {
/* Add your method from Part 1 here */
public static String decTextAbbr(String textAbbr) {
String IDK = "I don't know";
String TMI = "too much information";
String BFF = "best friends forever";
String IMHO = "in my humble opinion";
String LOL = "laughing out loud";
if(textAbbr.equals("LOL")) {
return LOL;
}
else if(textAbbr.equals("IDK")) {
return IDK;
}
else if(textAbbr.equals("BFF")) {
return BFF;
}
else if(textAbbr.equals("IMHO")) {
return IMHO;
}
else if(textAbbr.equals("TMI")) {
return TMI;
}
else {
return "Unknown";
}
}
public static void main(String[] args) {
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.indexOf("BFF") != -1)
System.out.println("Replaced "BFF" with ""+decTextAbbr("BFF")+"".");
if (inString.indexOf("IDK") != -1)
System.out.println("Replaced "IDK" with ""+decTextAbbr("IDK")+"".");
if (inString.indexOf("TMI") != -1)
System.out.println("Replaced "TMI" with ""+decTextAbbr("TMI")+"".");
if (inString.indexOf("LOL") != -1)
System.out.println("Replaced "LOL" with ""+decTextAbbr("LOL")+"".");
if (inString.indexOf("IMHO") != -1)
System.out.println("Replaced "IMHO" with ""+decTextAbbr("IMHO")+"".");
System.out.println();
System.out.println("Expanded: "+message);
return;
}
public static String newMessage(String str)
{
String Str = str;
Str = Str.replace("BFF", decTextAbbr("BFF"));
Str = Str.replace("IDK", decTextAbbr("IDK"));
Str = Str.replace("IMHO", decTextAbbr("IMHO"));
Str = Str.replace("TMI", decTextAbbr("TMI"));
Str = Str.replace("LOL", decTextAbbr("LOL"));
return Str;
}
}
Output:
Enter text:IDK how that happened. LOL.
You entered:IDK how that happened. LOL.
Replaced "IDK" with "I don't know".
Replaced "LOL" with "laughing out loud".
Expanded: I don't know how that happened. laughing out loud.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.