Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with my program. i am asking for an explanation on how the stringbui

ID: 3557836 • Letter: I

Question

I need help with my program. i am asking for an explanation on how the stringbuilder works and also the logic behind this program from my class name to my main class. This program needs to ask the user to enter a string and then ask the user to enter a letter that he wants to change on the string follow by ask the user to enter another letter that he wants to change it for and then output the same string but with the changes of the letter that user picked. i have created most of the program and it compiles but when it needs to output the new string i get an error.. please modify my code i dont want a brand new one.

public class Replacer
{
    String originalString;
    StringBuilder convertedString;
   
    public Replacer(String inOriginalString)
    {
        originalString = new String(inOriginalString);
        convertedString = new StringBuilder(inOriginalString);
    }
   
    public void replace(char chrToBeReplaced, String chrToReplaceWith )
    {
        //originalString = new String(inOriginalString);
       
        for (int i= 0 ; i < originalString.length(); i++)
        {
            if(originalString.charAt(i) == chrToReplaceWith.charAt(i));
            {
            convertedString.replace(i,i+1,chrToReplaceWith);
            }
        }
        //System.out.println(convertedString);
    }
   
    public void setReplacer(String inOriginalString)
    {

        originalString = inOriginalString;
       
    }
   
     public String getOriginalString()
     {
         return originalString;
        }
       
       
    public StringBuilder getConvertedString()
    {
        return convertedString;
       
    }
   
   
   
}

main class

import java.util.Scanner;

public class Driver
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
       
        String originalString;
        String convertedString;
        String newString;
       
        String StoreString;
        char characterToReplace;
        String newCharacter;
       
        System.out.print("Enter a sentence");
        originalString = input.nextLine();
       
        System.out.print("What letter do you want to replace? ");
        StoreString = input.nextLine();
       
        characterToReplace = StoreString.charAt(0);
       
        System.out.print("Ok, now what is the new letter? " );
        newCharacter = input.nextLine();
       
        //newCharacter = newString.charAt(0);
       
        Replacer practice;
        practice = new Replacer(originalString);
       
       
        System.out.print(" Here it is: ");
        practice.replace(characterToReplace, newCharacter);
       
        System.out.println(practice.getConvertedString());
    }
       
}

Explanation / Answer

// Note the problem is in the replace method

class Replacer
{
String originalString;
StringBuilder convertedString;

public Replacer(String inOriginalString)
{
originalString = new String(inOriginalString);
convertedString = new StringBuilder(inOriginalString);
}

public void replace(char chrToBeReplaced, String chrToReplaceWith )
{
//originalString = new String(inOriginalString);

for (int i= 0 ; i < originalString.length(); i++)
{
if(originalString.charAt(i) == chrToBeReplaced)
{
convertedString.replace(i,i+1,chrToReplaceWith);
}
}
//System.out.println(convertedString);
}

public void setReplacer(String inOriginalString)
{
originalString = inOriginalString;

}

public String getOriginalString()
{
return originalString;
}


public StringBuilder getConvertedString()
{
return convertedString;

}



}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote