String replace with integer not working

1.4k views Asked by At

I have been working on some Java code and I came across a really weird error, Im trying to replace a piece of string with an Integer. Added all kinds of debugs but I cant seem to find it out.

private void executeMsg() {
    value = value.replaceAll("(&([a-f0-9]))", "ยง$2");

    String border = tostiuhc.getWorldManager().getBorderSize() - tostiuhc.getShrinksize() + "";
    String time = tostiuhc.getPhase().getMinutes() + "";

    System.out.println("BORDER: " + border);
    System.out.println("TIME: " + time);

    value = value.replace("-border-", border + "");
    value = value.replace("-time-", time + "");

    tostiuhc.broadcast(value + " " + time);
}

As you can see, I create new String called 'time' and print it out 'TIME: value'. The original string that I'm changing is: The event is now -time- minutes in!

enter image description here

enter image description here

The problem here is that System.out.println("TIME:") shows the correct value, but the .replace just DOESNT work. I cant get my head around this, anyone has any idea?

1

There are 1 answers

5
shmosel On BEST ANSWER

The replace only works on the first execution. After that, value no longer contains -time-. As a solution, you can use a temporary variable to do your replacement:

String displayValue = value.replace("-time-", time + "");