0

I am new to Java and am trying to make a program that will take a string 'names' and replace the names of the people mentioned in that string with a nickname from another string 'replacement'. For instance, "John and Robert were on the way to meet Elizabeth" should return "Joe and Bob were on the way to meet Liz".

My issue is that whenever I run my program, I get multiple sentences back with only one name replaced in each (I'm guessing it is creating a new string each time). Is there a way to avoid this and have it all printed in one sentence? Could I use regex on 'replacement' somehow to fix it?

public static string namereplacer(String names, String replacement) {
    String[] toreplace = replacement.split("\t|\n");
    for (int i = 0; i <= toreplace.length/2; i++) {
        if (i % 2 == 0) { //take even values
        fixed; += names.replaceAll(toreplace[i+1],toreplace[i]);
        }
    }
    return fixed;
}

public static void main (String[] args) {
    namereplacer("John and Robert were on the way to meet Elizabeth", "John\tJoe\nRobert\tBob\nElizabeth\tLiz");
}

2 Answers 2

2

I couldn't run the code you have submitted because the variable fixed isn't declared and i suspect the line
fixed; += names.replaceAll(toreplace[i+1],toreplace[i]);
is supposed to be
fixed += names.replaceAll(toreplace[i+1],toreplace[i]);
because that would somewhat explain why you are getting multiple sentences, since you are replacing things multiple times and adding the result to fixed.

That being said I have noticed that when you are replacing the names with names.replaceAll(toreplace[i+1],toreplace[i]); you should be doing names.replaceAll(toreplace[i],toreplace[i+1]); since you would like to replace John for Joe, Robert for Bob and Elizabeth for Liz, not the other way around (Joe for John). Basically you want to replace the "even" with the "odd" so if you are selecting the even values of i you you like to replace those with i+1.

Also when iterating the array in the for loop with
for (int i = 0; i <= toreplace.length/2; i++)
you should be doing
for (int i = 0; i <= toreplace.length; i++)
because you want all the "evens" not just half of them, i think you might have switched up yourself a bit here with the evens logic and replacing half...

So to make your code work like you intend to you should replace the following lines:
for (int i = 0; i <= toreplace.length/2; i++) for
for (int i = 0; i <= toreplace.length; i++)

fixed; += names.replaceAll(toreplace[i+1],toreplace[i]); for
fixed = fixed.replaceAll(toreplace[i+1],toreplace[i]);

and also define the variable fixed beforehand Sting fixed = names; so that it has the same value as names.

Furthermore you can also improve your code a little bit:
A for is constituted by three statements for(statement1; statement2; stament3)
statement1 is executed a single time before the code block (what's inside the for)
statement2 defines the condition for executing the code block, the for will run while this statement is true
statement3 is executed everytime after the code block has been executed by the for loop

Generaly you will find and write foor loops like for(i=0; i!=foo; i++) where statement1 is used for variable initialization, statement2 for condition and statement3 for increment, but that doens't mean it is all you can do with it, in reality you can write whatever you want/need in those statements.
In you case you have

for (int i = 0; i <= toreplace.length/2; i++) {
        if (i % 2 == 0) { //take even values
        fixed; += names.replaceAll(toreplace[i+1],toreplace[i]);
        }
    } 

This means you are iterating over the toreplace array, taking the "evens" and replacing them with the "odds" on the fixed string, however inside the for loop you are selecting only half of the iterations you are making with if (i % 2 == 0) and you don't really need to do that since you can just choose to iterate by 2 instead of iterating all of them and only selecting half of them to do the replacement.
To do this you simply declare for (int i = 0; i != toreplace.length; i+=2) or the long way i=i+2 if you prefer, and you no longer need the if statement because you know you will be iteraing over all the even numbers.
Hope this helped you in some way.

Sign up to request clarification or add additional context in comments.

Comments

-1

Change the method namereplacer to this

public static String namereplacer(String names, String replacement) {
    String[] toreplace = replacement.split("\n");
    for(String str: toreplace){
        String [] keyValue = str.split("\t");
        if(keyValue.length != 2)
            continue;
        names = names.replaceAll(keyValue[0], keyValue[1]);
    }
    return names;
}

2 Comments

One issue with this answer is that it is almost all code with very little text and no explanation. Note that answers are often judged by how helpful they are to future visitors (one reason why answers to poor questions sometimes deserve up-voting). Please consider improving it.
Can you give some edit suggestion. So I can learn what did I do wrong while posting this answer ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.