-3

I'm working on a assignment in my com s class which requires me to create a wheel of fortune game. I'm currently working on the getDisplayedPhrase method which I will explain. So for this program I have a random phrase for example
"this is a question, thanks for helping!"
I want this phrase to change to
"**** ** * ********, ****** *** *******!"
This is how the phrase should look like before they guess it. As you can see I'm trying to only change the letters so I created a

private static final String alpha ="abcdefghijklmnopqrstuvwxyz" 

to avoid any punctuation. This is what I have so far:

public String getDisplayedPhrase() {
    for (int i = 0; i<secretPhrase.length(); i++){
        I don't know what to put here and what method to use???
                I'm thinking of using charAt() or indexOf()
    }
    return displayedPhrase;
}
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Andy9673
  • 47
  • 5
  • 6
    rather than saying I don't know, take a leap and try using charAt() and indexOf() and then get stuck. – Arham Oct 30 '12 at 21:44

3 Answers3

6
return secretPhrase.replaceAll("[a-zA-Z]","*")
Clint
  • 8,988
  • 1
  • 26
  • 40
  • or `\w` if 0-9 are to be transformed as well. – Adam Oct 30 '12 at 21:45
  • 1
    Exercise for the reader - extend to cope with accented characters - `les dégâts sur la Côte est en vidéo` would become `** **é**â** ** ** **ô** ** ** **é**` using `\w` – DNA Oct 30 '12 at 21:55
  • Java regex has poor support for localization. [link](http://stackoverflow.com/questions/4304928/unicode-equivalents-for-w-and-b-in-java-regular-expressions) – le3th4x0rbot Nov 01 '12 at 23:42
3

you can use Character class to determine if a character is alphabetic.

   String s = "this is a question, thanks for helping!";
            StringBuilder rep="";
            for(int i=0; i<s.length();i++){
                if(Character.isAlphabetic(s.charAt(i))){
                    rep.append("*");
                }
                else {
                    rep.append(s.charAt(i));
                }
            }
            System.out.println(rep);

You can also use String.replace() and replace the existing String instead of an extra new String

  for(int i=0; i<s.length();i++){
            if(Character.isAlphabetic(s.charAt(i))){
                s=s.replace(s.charAt(i), '*');
            }

        }
        System.out.println(s);

Output:

**** ** * ********, ****** *** *******!
PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • This creates dozens of unnecessary temporary Strings, use a StringBuilder – daniel gratzer Oct 30 '12 at 21:41
  • @jozefg true that, just edited it thanks.. :) – PermGenError Oct 30 '12 at 21:43
  • 1
    Good idea showing the non-regex answer, since it is what the OP actually asked about. :) – le3th4x0rbot Oct 30 '12 at 21:50
  • @BaileyS Did I miss something? Where did the OP mention he wants to have a non-regex answer? – Alexis Pigeon Oct 30 '12 at 21:53
  • @AlexisPigeon The small amount of code in the question shows a for loop iterating over the String, and inside of it he says "I'm thinking of using charAt() or indexOf()". So yes, he is thinking of a non-regex answer. You will see my answer is the regex one too... it is the better way to do it. Still, sometimes people are curious about one way even though it is not the best, if only to think of how it was done. – le3th4x0rbot Oct 30 '12 at 21:55
2

Use String.replaceAll(String regex, String replacement). Since it's an assignment, I'll let you do the research for the regex part.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44