How can I efficiently replace common text message abbreviations in Java?

  • Context: Java 
  • Thread starter Thread starter obeying
  • Start date Start date
  • Tags Tags
    Java Text
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 6K views
obeying
Messages
8
Reaction score
0
Hi everyone, I've been working on this assignment for a few days now with no luck at all. Would anyone be willing to review this code for me and see if I'm even setting it up right? Here are the directions for the assignment.

1) Use scnr.nextLine(); to get a line of user input into a string. Output that line.
2) Expand common text message abbreviations. Output a message for each abbreviation that is expanded, then output the expanded line. Note: Check for abbreviations in the order provided below.
Support these abbreviations (you only need to support these):

BFF -- best friend forever
IDK -- I don't know
JK -- just kidding
TMI -- too much information
TTYL -- talk to you later

End result of the code should look like:

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.

Code I have so far:

Code:
import java.util.Scanner;
public class TextMsgExpander
{
   public static void main(String[ ] args)
   {
        Scanner in;
        in = new Scanner(System.in);
	String txtMsg,mesg,replaced;
      
       String BFF="best friend forever";
       String IDK="i don't know";
       String JK=“just kidding“;
       String TMI=“too much information“;
       String TTYL="talk to you later";
       
       System.out.print("Enter Text :");
       txtMsg=sc.nextLine();
         
       System.out.println("You Entered :"+txtMsg);

       if(txtMsg.contains("BFF"))
       {
           txtMsg=txtMsg.replace("BFF",BFF);
           System.out.println("Replaced 'BFF' with "+BFF);
       }

       if(txtMsg.contains("IDK"))
       {
           txtMsg=txtMsg.replace("IDK",IDK);
           System.out.println("Replaced 'IDK' with "+IDK);
       }

       if(txtMsg.contains(“JK”))
       {
           txtMsg=txtMsg.replace(“JK”,JK);
           System.out.println("Replaced ‘JK’ with "+JK);
       }

       if(txtMsg.contains("TMI"))
       {
           txtMsg=txtMsg.replace("TMI",TMI);
           System.out.println("Replaced 'TMI' with "+TMI);
       }

       if(txtMsg.contains("TTYL"))
       {
           txtMsg=txtMsg.replace("TTYL",TTYL);
           System.out.println("Replaced 'TTYL' with "+TTYL);
       }
  
  

	System.out.println("Expanded :"+txtMsg);
  	 }

	}
 
Physics news on Phys.org
obeying said:
Hi everyone, I've been working on this assignment for a few days now with no luck at all. Would anyone be willing to review this code for me and see if I'm even setting it up right? Here are the directions for the assignment.

1) Use scnr.nextLine(); to get a line of user input into a string. Output that line.
2) Expand common text message abbreviations. Output a message for each abbreviation that is expanded, then output the expanded line. Note: Check for abbreviations in the order provided below.
Support these abbreviations (you only need to support these):

BFF -- best friend forever
IDK -- I don't know
JK -- just kidding
TMI -- too much information
TTYL -- talk to you later

End result of the code should look like:

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.

Code I have so far:

Code:
import java.util.Scanner;
public class TextMsgExpander
{
   public static void main(String[ ] args)
   {
        Scanner in;
        in = new Scanner(System.in);
	String txtMsg,mesg,replaced;
      
       String BFF="best friend forever";
       String IDK="i don't know";
       String JK=“just kidding“;
       String TMI=“too much information“;
       String TTYL="talk to you later";
       
       System.out.print("Enter Text :");
       txtMsg=sc.nextLine();
         
       System.out.println("You Entered :"+txtMsg);

       if(txtMsg.contains("BFF"))
       {
           txtMsg=txtMsg.replace("BFF",BFF);
           System.out.println("Replaced 'BFF' with "+BFF);
       }

       if(txtMsg.contains("IDK"))
       {
           txtMsg=txtMsg.replace("IDK",IDK);
           System.out.println("Replaced 'IDK' with "+IDK);
       }

       if(txtMsg.contains(“JK”))
       {
           txtMsg=txtMsg.replace(“JK”,JK);
           System.out.println("Replaced ‘JK’ with "+JK);
       }

       if(txtMsg.contains("TMI"))
       {
           txtMsg=txtMsg.replace("TMI",TMI);
           System.out.println("Replaced 'TMI' with "+TMI);
       }

       if(txtMsg.contains("TTYL"))
       {
           txtMsg=txtMsg.replace("TTYL",TTYL);
           System.out.println("Replaced 'TTYL' with "+TTYL);
       }
  
  

	System.out.println("Expanded :"+txtMsg);
  	 }

	}

Hi obeying!

It seems fine to me, except that it currently won't compile, since the variable [m]sc[/m] is used, but it does not exist.
Oh, and there should be a couple of empty lines in the output according to the example output.

Btw, personally I'd put those abbreviations in a collection, and loop over the collection, rather than writing a series of consecutive if-statements.
 
Also netbeans Java. the most commonly used compiler, has the String operator "replace(String oldstring, String newstring)" such that, given a String named "s", s.replace("IDK", "I don't know") will replace every instance of "IDK" in String s with "I don't know". Using that these substitutions can be done in 5 lines and you do not need to use "if" or a loop.
 
Last edited by a moderator:
HallsofIvy said:
Also netbeans Java. the most commonly used compiler, has the String operator "replace(String oldstring, String newstring)" such that, given a String named "s", s.replace("IDK", "I don't know") will replace every instance of "IDK" in String s with "I don't know". Using that these substitutions can be done in 5 lines and you do not need to use "if" or a loop.

Erm... aren't we already using that replace() function?
And no, it's not netbeans specific. It's part of the java specification.
If it were netbeans specific, we shouldn't use it, since it would break portability.
Luckily, there's no such thing as netbeans specific java code.
Furthermore, netbeans is not a compiler - it's just one of many IDE's that uses the java compiler from a JDK.
And at least for instance according this article, it's not the most commonly used, Eclipse is.

Btw, the problem statement asks to display a line for each abbreviation that is replaced, so we still need the if.
Even worse, they have to be replaced in the given order, so we can't even use a [M]HashMap[/M].

TL;DR we can't make this shorter, other than replace the sequence of if-statements by a loop with an if-statement.
And the best we can do is use a collection that implements [M]SortedMap[/M], since the abbreviations are in alphabetical order.