3

The goal of this program is to prompt the user for a single character and a phrase, and then replace any instances of that character within that phrase with a '$'. My program below does just that, but when I showed it to my professor I was told that I cannot use .replace in the methods I built, so I have to figure out a way to not use that. I have worked at it for a while, and thus far I know that I can replace it with a for loop, but after several frustrating iterations, I can't seem to get it right. Excuse me if my code looks funky, I am still an introductory java student so I'm still learning the basics. I have provided a proposed solution at the end of my code snippet below.

public static char getKeyCharacter(String userInput) {

    char keyCharacter;
    Scanner inputStream = new Scanner(System.in);
    while(userInput.length() > 1) 
    {
        System.out.println("Please enter a SINGLE character to use as key: ");
        userInput = inputStream.nextLine();
    }
    keyCharacter = userInput.charAt(0);

    return keyCharacter;
}
public static String getString(String userResponse) {

    Scanner inputStream = new Scanner(System.in);
    String theString;
    while(userResponse.length() > 500) {
        System.out.println("Please enter a phrase or sentence >= 4 and <=500 characters: ");
        userResponse = inputStream.nextLine();
    }
    while(userResponse.length() < 4) {
        System.out.println("Please enter a phrase or sentence >= 4 and <=500 characters: ");
        userResponse = inputStream.nextLine();

    }

    theString = userResponse;
    return theString;

}
public static String maskCharacter(String theString, char keyCharacter){

    String maskedString = "";
    final char mask = '$';
    maskedString = maskedString + theString.replace(keyCharacter, mask);
    System.out.println("String with " + keyCharacter + " masked: ");
    return maskedString;
}

public static String removeCharacter(String theString, char keyCharacter) {

    String modifiedString = " ";
    final char replaceChar = ' ';
    modifiedString = modifiedString + theString.replace(keyCharacter, replaceChar);
    System.out.println("String with " + keyCharacter + " removed:");
    return modifiedString;

}

public static int countKey(String theString, char keyCharacter) {
    int charCount = 0;
    for (int c = 0; c < theString.length(); c++) {
        if (theString.charAt(c) == keyCharacter) {
            charCount++;
        }
    }
    System.out.println("Occurences of " + keyCharacter + " in string:");
    return charCount;
}  

}

I believe the solution is will look something like this, but thus far I've been unsuccesful -

public static String maskCharacter(String theString, char keyCharacter){

    String maskedString = "";
    final char mask = '$';
    for (int k = 0; k < theString.length(); k++) {
        if (theString.charAt(k) == keyCharacter) {
            keyCharacter = mask;
         }
    System.out.println("String with " + keyCharacter + " masked: ");
    return maskedString;
}

My issue lies in making the maskedString = theString with all the keyCharacters replaced by mask. For the record, I have yet to learn anything about those fancy arrays, so if there is a way to do this using a simple for loop I would greatly appreciate it. Thank you for the assistance in advance!

5
  • 2
    Strings are immutable in Java. You don't "replace" characters in a string, you build a new String (with StringBuilder, for instance) that contains the original string with the appropriate characters replaced. Commented Feb 13, 2018 at 5:52
  • well, have you asked the professor what to use instead? I think it is HIS job to teach you... and replace is the usual and probably most indicated way to solve such a problem. Commented Feb 13, 2018 at 6:29
  • @JimGarrison, thank you, that's good information to know! I am looking at StringBuilder right now just for my knowledge. Commented Feb 13, 2018 at 6:56
  • @CarlosHeuberger I asked him, and he said my best bet is to use a for loop, which after many failures I turned her for help. I think he's not letting us use it just so we get some extra practice. Commented Feb 13, 2018 at 6:57
  • Not an exact a duplicate, but anything over here should work. Commented Feb 13, 2018 at 15:48

4 Answers 4

4

I would use a StringBuilder and String#toCharArray() with a simple for-each loop. Like,

public static String maskCharacter(String theString, char keyCharacter){
    StringBuilder sb = new StringBuilder();
    for (char ch : theString.toCharArray()) {
        if (ch == keyCharacter) {
            sb.append('$'); // <-- mask keyCharacter(s).
        } else {
            sb.append(ch); // <-- it isn't the character to mask
        }
    }
    return sb.toString();
}
Sign up to request clarification or add additional context in comments.

Comments

3

I wouldn't use a StringBuilder: just use the result of toCharArray() directly:

char[] cs = theString.toCharArray();
for (int i = 0; i < cs.length; ++i) {
  if (cs[i] == keyCharacter) cs[i] = '$';
}
return new String(cs);

Not only is it more concise, but:

  • It will run faster, because it's cheaper to access an array element than to invoke a method; and because it doesn't require StringBuilder's internal buffer to resize (although you could just pre-size that);
  • It will use less memory, because it doesn't require storage for the copy inside StringBuilder.

Comments

1

public static String maskCharacter(String theString, char keyCharacter){

String masked = "";
for (int i = 0 ; i < theString.length() ; i++) {

    if (theString.charAt(i) == keyCharacter) {
         masked += "$";
    } 
    else {
        masked+=theString.charAt(i)+"";
    }
}
return masked;

}

Comments

0

An answer that only uses string concatenation and basic character access.

You seem to know that you can concatenate something to a string and get a different string.

maskedString = maskedString + ...;

You also know you can build a for-loop that gets each individual character using .charAt()

for (int k = 0; k < theString.length(); k++) {
    char nch = theString.charAt(k);
}

You can check equality between chars

if (nch == keyCharacter)

... assuming you know about else-branches, isn't it clear you just need to put them together?

if (nch == keyCharacter) {
    // append '$' to maskedString
}
else {
    // append nch to maskedString
}

Of course this creates a new string on every loop iteration so it is not terribly efficient. But I don't think that's the point of the exercise.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.