Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: "?" replacement

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default "?" replacement

    I'm making the encryption program mentioned in my other post. When I type all other character besides " ' /\ and . it works fine besides the question mark. It gives me this error when ever i type in a question mark:

    Options:
    ---------------
    |1. Encryption|
    |2. Decryption|
    ---------------

    Enter your choice:
    1

    Enter the phrase you want encrypted:
    ?
    Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta
    character '?' near index 0
    ?
    ^
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.sequence(Unknown Source)
    at java.util.regex.Pattern.expr(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.lang.String.replaceAll(Unknown Source)
    at encryption.main(encryption.java:376)
    Press any key to continue . . .
    This is the code for replacing the ?:
    			if (pdphrase.contains("?")) {
     
    				pdphrase = pdphrase.replaceAll("?", "24");
     
    			}

    I don't know whats going on or if I should just scrap that character like i did others. The period caused every number to change it its number and the other ones caused errors when compiling. Please Help!
    Last edited by sp11k3t3ht3rd; January 10th, 2011 at 08:52 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: "?" replacement

    String.replace uses Java's Regex engine which uses those characters (actually, I think there are a few other characters such as *) for special behaviors.

    The simple solution is to manually search the string yourself, or to ensure that the user enters a valid Java Regex pattern. See: Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)

  3. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    javapenguin (March 26th, 2011), sp11k3t3ht3rd (January 10th, 2011)

  4. #3
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: "?" replacement

    Alright thanks! I might just take out the characters that are used in the Java Regex engine. This is only for my sophomore level science fair so it won't matter and I dont want peoples heads exploding when I try to explain it to them in my presentation...

  5. #4
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: "?" replacement

    "?" is special to regular expression. try escaping it. "\\?"

  6. The Following User Says Thank You to JavaHater For This Useful Post:

    javapenguin (March 26th, 2011)

Similar Threads

  1. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  2. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  3. Replies: 1
    Last Post: March 31st, 2010, 09:42 PM
  4. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  5. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM