Addison
Addison

Reputation: 177

Java escaped sequence syntax error

I'm trying to create a string array with lines of codes, so that the program can over-write a portion of existing code once it hits a mark. My problem comes on this line:

var finalTitle = (str.replace("()", ("(" + num + ")")));

As I am attempting to convert that line into a valid string, I understand that the quotation mark can be somewhat tricky to parse. This is what I have so far:

"var finalTitle = (str.replace(\"()\", (\"(\" + num + \")\")));"

However, eclipse will not stop complaining that the syntax of this line is incorrect. Does anyone know how to correctly format this line? Or possibly more specifically, how to parse quotation marks into a string?

Here is an example of the string array I am filling. I am going to loop through it with each iteration writing a new line to the .js file:

String[] lines = {"var patientTree = getPatientMenuTree();", "var rootNode = patientTree.getNodeById('Patients');", "var str = rootNode.title;", "var num = patientArray.length;", "var finalTitle = (str.replace(\"()\", (\"(\" + num + \")\")));" };

Upvotes: 0

Views: 253

Answers (1)

Mark Byers
Mark Byers

Reputation: 838156

It should work fine. Try assigning to a variable:

String text = "var finalTitle = (str.replace(\"()\", (\"(\" + num + \")\")));";

Update

Near the end of the line you have an extra };" that shouldn't be there:

String[] lines = {"...", "...", "...", "..." };" };

I see you already fixed that error.

Upvotes: 1

Related Questions