-2

I am trying to get my output to display double quotations around the abbreviations and also the translated abbreviations. However I have not covered escape sequences in my current class so I was wondering if there was another way to accomplish this. The workbook will not accept when I try with escape sequence.

I have tried escape sequence and using two single quotes ('' '') but neither have worked. Perhaps I am missing something and am fairly new to the java language. Just trying to learn the most efficient way from a fundamental standpoint.

import java.util.Scanner;

public class TextMsgExpander { public static void main(String[] args) {

Scanner scnr = new Scanner(System.in);

String txtMsg;
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.println("Enter text: ");
txtMsg = scnr.nextLine();

System.out.println("You entered: " + txtMsg);
System.out.println();

if(txtMsg.contains("BFF")) {
  txtMsg = txtMsg.replace("BFF", BFF);
  System.out.println("Replaced BFF with " + BFF);
}                    // above line is where I tried escape sequence

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

System.out.println();

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

return;

} }

Your output

Enter text: 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.

Expected output

Enter text: 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.

jhelphenstine
  • 435
  • 2
  • 10
Jmoore
  • 3
  • 2
  • 3
    [Is this what you've previously tried?](https://stackoverflow.com/questions/3844595/how-can-i-make-java-print-quotes-like-hello) – jhelphenstine Jul 17 '19 at 21:46
  • There are other ways of doing this, including using a StringBuilder and appending the `"` character, but the most efficient way is to use the `\ ` escape character: `\"` is how you escape double quotes. – Jason Jul 17 '19 at 22:20

3 Answers3

0

Have you tried this:

\"example text\"

So you would have something like this:

  System.out.println("Replaced \"BFF\" with " + "\"" + BFF + "\"");

or

  System.out.println("Replaced \"BFF\" with \"" + BFF + "\"");
cluelessdev
  • 187
  • 1
  • 5
  • I apologize to all who helped the previous attempt with escape sequence I formatted improperly however this worked this time. Thank you all for your help and guidance. – Jmoore Jul 18 '19 at 13:38
0

Normally it should work with escape characters. Have u tried something like this:

 System.out.println("\"These two semi colons are removed when i am printed\"");

I tested it and it worked for me.

Sascha
  • 89
  • 7
0

If you cannot use \ escape sequences, for whatever reason, you can use the fact that an ' apostrophe doesn't need to be escaped in a "xx" string literal, and that a " double-quote doesn't need to be escaped in a 'x' character literal.

E.g. to print Replacing "foo" with 'bar' was easy, and foo and bar are from variables, you can do this:

String s = "Replacing " + '"' + foo + '"' + " with '" + bar + "' was easy"`;
Andreas
  • 154,647
  • 11
  • 152
  • 247