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

  • Thread starter Thread starter obeying
  • Start date Start date
  • Tags Tags
    Java Text
AI Thread Summary
The discussion centers around a coding assignment that requires users to create a Java program that takes a line of text input, identifies specific text message abbreviations, expands them, and outputs the results. The provided code has several issues, including the use of an undefined variable "sc" instead of "in" for input, and it lacks the necessary formatting for output. Participants suggest improvements, such as using a collection to manage abbreviations and looping through them instead of writing multiple if-statements. They clarify that the replace() function is part of the Java specification and not specific to any IDE, emphasizing the need to maintain the order of replacements as specified in the assignment. The discussion highlights the importance of adhering to the assignment's requirements while also considering code efficiency and clarity.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

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