Gustavo C
Gustavo C

Reputation: 15

Replace a certain character from a String

In this code, I am trying to replace a letter from a String entered by a user. For example, the program asks for a word, and the user enters "hello", the next variable that the user inputs is the letter that is going to be replaced ("l"), and the final variable is the letter that is going to replace the previous variable ("x").

The result should be "hexxo", but my program is only replacing one of the l's, what is wrong with my program?

import java.util.Scanner;
public class Letter
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter your word:");
        String word = input.nextLine();
        
        System.out.println("\nEnter the letter you want to replace:");
        String replace = input.nextLine();
        
        System.out.println("\nEnter the replacing letter:");
        String add = input.nextLine();
       
        System.out.println(replaceLetter(word, replace, add));
    }
    
    public static String replaceLetter(String word, String letterToReplace, String add)
    {
        String newWord = "";
        for(int i = 0; i < word.length(); i++)
        {
            if(word.substring(i, i+1).equals(letterToReplace))
            {
            String front = word.substring(0, word.indexOf(letterToReplace));
            String back = word.substring(word.indexOf(letterToReplace)+1);
            newWord = front + add + back;
            }
            
        }
        return newWord;
        
        
    }
}

Upvotes: 0

Views: 140

Answers (5)

Ashish Karn
Ashish Karn

Reputation: 1143

Because you are using word.indexOf(letterToReplace) which will always return the first index of string. So, Instead, you need to use word.indexOf(letterToReplace,i); with minor code, changes will help you to solve your problem. Try it yourself.

Happy Coding !!!

Upvotes: -1

Deepak Tatyaji Ahire
Deepak Tatyaji Ahire

Reputation: 5321

Every time the loop checks the the original string word.

First, your program updates newWord every-time, but whenever newWord is updated, it is calculated with not-updated word.

Just remove the variable newWord and perform operations on variable word only as presented in the following code:

import java.util.Scanner;
public class Letter
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter your word:");
        String word = input.nextLine();
        
        System.out.println("\nEnter the letter you want to replace:");
        String replace = input.nextLine();
        
        System.out.println("\nEnter the replacing letter:");
        String add = input.nextLine();
    
        System.out.println(replaceLetter(word, replace, add));
    }
    
    public static String replaceLetter(String word, String letterToReplace, String add)
    {
        
        for(int i = 0; i < word.length(); i++)
        {
            if(word.substring(i, i+1).equals(letterToReplace))
            {
                String front = word.substring(0, word.indexOf(letterToReplace));
                String back = word.substring(word.indexOf(letterToReplace)+1);
                word = front + add + back;
            }
            
        }
        return word;
        
        
    }
}

Upvotes: 1

genius42
genius42

Reputation: 243

The reason why your algorithm is not working, is that you're assigning a new String to newWord every time you replace a letter. So you will only get an output of the last replacement.

Upvotes: 1

Naetmul
Naetmul

Reputation: 15592

There are 2 problems.

First, your program updates newWord everytime,
but whenever newWord is updated, it is calculated with not-updated word.

if(word.substring(i, i+1).equals(letterToReplace))
{
    String front = !!!!!word!!!!!.substring(0, word.indexOf(letterToReplace));
    String back = !!!!!word!!!!!.substring(word.indexOf(letterToReplace)+1);
    newWord = front + add + back;
}

Second, why substring() in the if block does not use i?

String front = word.substring(0, !!!!!word.indexOf(letterToReplace)!!!!!);
String back = word.substring(!!!!!word.indexOf(letterToReplace)+1!!!!!);

Upvotes: 2

genius42
genius42

Reputation: 243

You could simply use String#replace or String#replaceAll. Just take a look into the java-doc to understand how they work.

Upvotes: 0

Related Questions