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
Click For Summary

Discussion Overview

The discussion revolves around a Java programming assignment focused on replacing common text message abbreviations with their full forms. Participants are reviewing code implementations, discussing best practices, and addressing compilation issues.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant shares their code and assignment requirements, seeking feedback on their implementation.
  • Another participant points out a compilation error due to the use of an undefined variable 'sc' instead of 'in'.
  • Suggestions are made to use a collection to store abbreviations and loop through them instead of using multiple if-statements.
  • Another participant mentions that the String method 'replace' can be used to perform replacements without needing if-statements or loops, but this is challenged by others.
  • It is clarified that the 'replace' method is not specific to NetBeans and is part of the Java specification, emphasizing the importance of portability in Java code.
  • Participants discuss the requirement to display a message for each abbreviation replaced and the necessity to maintain the order of replacements as specified in the assignment.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to implement the assignment. While some suggest using collections and loops, others argue for maintaining the current structure due to assignment constraints. There is no consensus on a single best method.

Contextual Notes

Participants note that the assignment requires specific output formatting and order of replacements, which may limit the use of certain programming techniques like HashMaps.

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);
  	 }

	}
 
Technology 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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
58
Views
13K
  • · Replies 3 ·
Replies
3
Views
3K