I need ths code to run as an if-else statement. No shortcuts what so ever and pl
ID: 3782669 • Letter: I
Question
I need ths code to run as an if-else statement. No shortcuts what so ever and please ensure the spacing is correct. Please help I am not getting this to compile properly. If there is a simpler way to create this code without anything added to what is below I would greatly appreciate that as well.
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;
}
}
Explanation / Answer
MessageExpander.java
import java.util.Scanner;
public class MessageExpander {
public static void main(String[] args)
{
//Declaring String variables
String txtMsg,mesg,replaced ;
//Scanner Object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
String BFF="best friend forever";
String JK="just kidding";
String IDK="i don't know";
String TMI="too much information";
String TTYL="talk to you later";
//Getting the inputs entered by the user.
System.out.print("Enter Text :");
txtMsg=sc.nextLine();
//Displaying the user entered Input String
System.out.println(" You Entered :"+txtMsg);
System.out.println();
// Check whether the entered string contains Particular string or not
if(txtMsg.contains("BFF"))
{
txtMsg=txtMsg.replace("BFF",BFF);
System.out.println("Replaced 'BFF' with ""+BFF+""");
}
// Check whether the entered string contains Particular string or not
if(txtMsg.contains("JK"))
{
txtMsg=txtMsg.replace("JK",JK);
System.out.println("Replaced 'JK' with ""+JK+""");
}
// Check whether the entered string contains Particular string or not
if(txtMsg.contains("IDK"))
{
txtMsg=txtMsg.replace("IDK",IDK);
System.out.println("Replaced 'IDK' with ""+IDK+""");
}
// Check whether the entered string contains Particular string or not
if(txtMsg.contains("TMI"))
{
txtMsg=txtMsg.replace("TMI",TMI);
System.out.println("Replaced 'TMI' with ""+TMI+""");
}
// Check whether the entered string contains Particular string or not
if(txtMsg.contains("TTYL"))
{
txtMsg=txtMsg.replace("TTYL",TTYL);
System.out.println("Replaced 'TTYL' with ""+TTYL+""");
}
System.out.println();
//Displaying the expanded form.
System.out.println("Expanded :"+txtMsg);
}
}
______________________
Compile
Command To compile the program : javac MessageExpander.java
Run:
Command To run the program : java MessageExpander
Output:
Enter Text :You are my BFF.Bye TTYL.
You Entered :You are my BFF.Bye TTYL.
Replaced 'BFF' with "best friend forever"
Replaced 'TTYL' with "talk to you later"
Expanded :You are my best friend forever.Bye talk to you later.
___________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.