• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using parenthesis inside a string  RSS feed

 
J Dunn
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the syntax for using a parenthesis inside a string? If I don't put an escape character on it, the compiler thinks its supposed to close a statement outside of the string... but if I do put on on it it says its an illegal escape character?

If nothing else I guess I can write the ascii code into a byte and then convert that to a string but there has to be a normal convention to do it.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but if I do put on on it it says its an illegal escape character?


Well, what escape character are you using? And I am assuming that you mean the quote, there is no reason to escape a parenthesis in a Java string.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This compiles and runs just fine:



C:\slop>java test
parenthesis ( inside ) a string

C:\slop>
 
J Dunn
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I do "(" it generates errors, since it thinks it is closing something outside. "\(" generates illegal escape character.

Compiling using javac in Linux if it matters.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps it would help if you showed us the code?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed. There is no reason to escape a parenthesis in a Java string -- it should work fine. Please show us the offending code.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
show the code, AND the exact text of the error message.

are you compiling from the command line, or are you using some IDE? I can't imagine any IDE would be so poorly written as to regard a parenthesis inside a string as a match to one outside, but who knows...
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by J Dunn:
When I do "(" it generates errors, since it thinks it is closing something outside. "\(" generates illegal escape character.

Compiling using javac in Linux if it matters.


You must be talking about a regex-String. Something like this I presume:



Which indeed will throw an exception since the regex sngine expects a closing parentheses. You will need to escape the '('. But whenever you put a single backslash inside a String literal, the compiler expects a character like 'n' (new line) , 't' (tab), a '"' (double quote), etc.

So you can't escape opening or closing parentheses with a single backslash. You will need to double escape them (or put them in a character class).

Demo:

 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Piet Verdriet:

Which indeed will throw an exception since the regex sngine expects a closing parentheses.


Which, on the other hand, has nothing to do with the compiler at all.

Another possibility is that there is a quote inside the string that isn't escaped.

It's really hard to help here without seeing the actual code.
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
Which, on the other hand, has nothing to do with the compiler at all.


No, what makes the compiler complain is when you try to use a single backslash to escape something in a regex, like this: The Java compiler tries to treat \( one of the String-literal escape sequences, and of course it fails.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The OP never mentioned regular expressions. And clearly stated that when "(" is used (without escapes), it generates errors, since it thinks it is closing something outside of the string.

So... it may be best to let the OP clarify a bit, and show us some code.

Henry
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
The OP never mentioned regular expressions. And clearly stated that when "(" is used (without escapes), it generates errors, since it thinks it is closing something outside of the string.

So... it may be best to let the OP clarify a bit, and show us some code.

Henry


I agree that the OP should have explained him/herself better, but I don't see how it cannot be a regex the OP is talking about.
From the original post of the OP: "If I don't put an escape character on it, the compiler thinks its supposed to close a statement outside of the string". Now, if that were a plain String, there wouldn't have been any error message at all. Of course, if that were completely true, it wouldn't have been a compile time error but a runtime error, but let's assume the OP received an error message after not escaping a parenthesis. If that is so, is must be a regex String. I don't see what else can throw an error/exception for not escaping a parenthesis.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!