• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Using parenthesis inside a string

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps it would help if you showed us the code?
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Yeah, but is it art? What do you think tiny ad?
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic