This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Illegal Escape Character Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Illegal Escape Character" Watch "Illegal Escape Character" New topic
Author

Illegal Escape Character

Faber Siagian
Ranch Hand

Joined: Jul 08, 2008
Posts: 52
Dear all,

I have a problem to understand this case :

import java.util.regex.*;



Compilation should be failed at line 7 because the presence of an Illegal escape character.
But if i change the program :

...
Pattern p = Pattern.compile(args[0]);
...

then i run the application by invoking command line :

java Regex "\d"

the application runs successfully, but actually i expect the compilation failed at line 7. Someone please explain this to me.


Sun Certified Programmer for the Java 2 Platform, Standard Edition 5.0 (88 %)
Sargunan Rswamy
Greenhorn

Joined: Jun 24, 2008
Posts: 3

[ July 14, 2008: Message edited by: sargunan Rswamy ]
Sargunan Rswamy
Greenhorn

Joined: Jun 24, 2008
Posts: 3
Try This





import java.util.regex.*;


[ July 14, 2008: Message edited by: sargunan Rswamy ]
Faber Siagian
Ranch Hand

Joined: Jul 08, 2008
Posts: 52
I can't see the reply
Sumit Bisht
Ranch Hand

Joined: Jul 02, 2008
Posts: 329

Your program is working fine!
You were expecting a RunTime Exception but your JVM decided that it was ok to go by a single \
Please note that this behaviour is not guaranteed in all JVMs.
For more, please see pg. 482 of k&b scjp 5 study guide.
Also as mentioned there, the command-line behaviour is not in the exam
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
Hi Faber ,

You have to make only one change if you are running this program
on Windows.

<blockquote>code:
<pre name="code" class="core">
Pattern p = Pattern.compile("\\d");
</pre>
</blockquote>


SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Illegal Escape Character