0

As a bit of background to the problem I load in a text file and then assign a phrase from that text file to become a randomPirateWord, I then change the letters into that text file to become **'s and that works correctly. However when I am asking the user to guess a letter, it doesn't work correctly, if they guess incorrectly then the code works fine but if they guess a letter correctly the code doesn't work properly. I have put the error message below the code:

if (!escape.equalsIgnoreCase("m")){
                    System.out.print(" Type the letter you want to guess: ");
                    char letter = input.nextLine().charAt(0); 

if(m.getRandomPirateWord().contains(letter+"")){
                        System.out.println(m.getRandomPirateWord().replaceAll("*",letter+""));
                    }

Error message:
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
*
^
    at java.util.regex.Pattern.error(Pattern.java:1924)
    at java.util.regex.Pattern.sequence(Pattern.java:2090)
    at java.util.regex.Pattern.expr(Pattern.java:1964)
    at java.util.regex.Pattern.compile(Pattern.java:1665)
    at java.util.regex.Pattern.<init>(Pattern.java:1337)
    at java.util.regex.Pattern.compile(Pattern.java:1022)
    at java.lang.String.replaceAll(String.java:2162)
    at uk.ac.aber.dcs.pirate_hangman.TextBasedGame.runTextBasedGame(TextBasedGame.java:45)
    at uk.ac.aber.dcs.pirate_hangman.Application.runApplication(Application.java:19)
    at uk.ac.aber.dcs.pirate_hangman.Main.main(Main.java:6)
Java Man
  • 1,854
  • 3
  • 21
  • 43
user228908
  • 31
  • 6
  • 1
    how is this related to Swing? – kleopatra Apr 25 '14 at 10:06
  • I guess simply googling the error or reading the things that popped up when you typed in the subject was out of the question? Duplicate of: http://stackoverflow.com/questions/917822/tokenizing-error-java-util-regex-patternsyntaxexception-dangling-metacharacter – Brian Roach Apr 25 '14 at 10:06
  • BTW, how are you going to decide which `*` to replace? Both `replace()` and `replaceAll()` will replace all the `*`s. – Rohit Jain Apr 25 '14 at 10:14
  • I'm really unsure how to determine which * to replace, do you have any recommendations? – user228908 Apr 25 '14 at 14:03

5 Answers5

5

Use String#replace() instead of String#replaceAll(). The later one uses regex pattern for replacement, where * is a meta-character, and needs to be escaped.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
1

Use the following, You have to escape the * character, since replaceAll() method accepts regular expression as one argument

replaceAll("\\*",letter+"")
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
  • When I do that it displays the whole word for example The phrase is: ****** * ***** *** I typed in: a And it printed out: you're a bilge rat – user228908 Apr 25 '14 at 10:09
  • @user228908 : I don't get you. – Abimaran Kugathasan Apr 25 '14 at 10:12
  • I originally have a word say for example hello, I then replace it into stars so it would become ***** I then ask the user for input of a guess of the letter so for example if they typed h I would want it do display h**** but for some reason if they guess a letter correctly it's displaying the whole word. Hope that makes more sense. – user228908 Apr 25 '14 at 10:14
  • 1
    @user228908 Read my comment on your question. I guess you're approaching this problem wrongly. You need to get the actual position of the letter guessed from the original string, and replace the `*` at that position only. Simple `replace()` will not work. – Rohit Jain Apr 25 '14 at 10:18
0

Check out below Example.

import java.io.*;

public class Test{
   public static void main(String args[]){
      String Str = new String("Welcome to Tutorialspoint.com");

      System.out.print("Return Value :" );
      System.out.println(Str.replaceAll("(.*)Tutorials(.*)",
                         "AMROOD" ));
   }
}

Syntax of method :

public String replaceAll(String regex, String replacement)
  • regex -- the regular expression to which this string is to be matched.

  • replacement -- the string which would replace found expression.

OUTPUT: AMROOD

Reference : Regex replacement

Java Man
  • 1,854
  • 3
  • 21
  • 43
-1

The problem is that * is a reserved character in regexes, so you need to escape it.

replaceAll("\\*",letter+"")
Asif Bhutto
  • 3,916
  • 1
  • 24
  • 21
-1

‘*’ symbol is used to identify a group from the regular expression which is the first parameter of ‘replaceAll’ or ‘replaceFirst’ method

1. Using ‘replace’ method: This would be the good choice if you want to replace a string literal and not a pattern.


2. Escaping ‘*’ symbol: If you need to a use regular expression, and your pattern has no groups identified, then you can escape any group identification symbols from your replace string as shown below:



String replaceValue = java.util.regex.Matcher.quoteReplacement("*100");
niiraj874u
  • 2,180
  • 1
  • 12
  • 19