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

  • Java
  • Thread starter obeying
  • Start date
  • Tags
    Java Text
In summary, This code asks the user to input a text message and then expands any common text message abbreviations, such as BFF, IDK, JK, TMI, and TTYL. The code then outputs the expanded message and any replacements that were made.
  • #1
obeying
8
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);
  	 }

	}
 
Technology news on Phys.org
  • #2
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.
 
  • #3
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:
  • #4
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.
 

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

What is a text message expander?

A text message expander is a program or application that automatically expands abbreviated words or phrases into their full form in a text message. This can save time and effort for the user by reducing the amount of typing required.

How does a text message expander work?

A text message expander works by using a predefined list of abbreviations and their corresponding full forms. When the user types an abbreviation in a text message, the program recognizes it and replaces it with the full form.

What are the benefits of using a text message expander?

The main benefit of using a text message expander is the time and effort saved by reducing the amount of typing required. It can also help with spelling and grammar by automatically inserting correct and complete words or phrases.

Can a text message expander be customized?

Yes, some text message expander programs allow for customization of the list of abbreviations and their corresponding full forms. This allows the user to add or remove abbreviations based on their preferences and needs.

Is a text message expander only useful for text messages?

No, a text message expander can be used in any situation where typing is involved, such as emails, documents, or social media posts. It can also be helpful for individuals with disabilities that make typing difficult or for those who want to increase their typing speed and efficiency.

Similar threads

Replies
1
Views
6K
Replies
1
Views
6K
Replies
1
Views
1K
Replies
1
Views
4K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
4
Views
2K
Back
Top