I am taking an intro level java programming class and i am a little stuck at the moment. I'm trying to use a the replace method. My objecti with this program is to gather a phrase from the user and then have it print the same phrase with some characters replaced. The Specific characters are 'a' to '@', 'e' to '3', 'i' to '!', 'o' to '0', 'b' to '8', and ' ', to '_'. I am keeping this as basic as possible so any comments I would appreciate if you keep in mind that anything too much more complex than what i have here will probably not be what i'm looking for. But please anything you can think of, whether it helps or not, will be much appreciated.

import java.util.Scanner;
public class Mutator
    public static void main (String[] args)
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Which of our presidents freed the slaves?");
        String phrase = keyboard.nextLine();

        phrase.replace(' ','_');


Remember that replace() returns a string. It doesn't modify the string that called it directly, but rather it makes a new String object. You're really close, but you need to keep that in mind.

So do I need to create a new variable for each character I'm replacing?

do I need to create a new variable

Try it and see what happens. You'll need a print statement for each variable.

Edited 4 Years Ago by NormR1

I suggest to put it to the same variable.

phrase = phrase.replace('a', '@');
Yes! that was exactly what i needed. That got me right where i wanted to be. Thanks for your help!!
This question has already been answered. Start a new discussion instead.