javaescapingsequencequotations

Is there an alternative way to generate quote marks without using the escape sequence?


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.


Solution

  • 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 + "\"");