Solved Cant replace text

Discussion in 'Plugin Development' started by 567legodude, Jul 22, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    567legodude

    I have an EventHandler to detect player deaths, inside that I have String killerMessage with the text "tell <killer> You killed a person"
    I also have String killer with a players name.
    I have the code
    Code:java
    1. killerMessage.replaceAll("<killer>", killer);

    Everything goes fine, but for some reason, it doesn't replace anything, it just comes out the same as the original string. Can someone tell me why the replaceAll() isn't working?
     
  2. Offline

    CorrieKay

    Strings are immutable objects in java. Which means if you have a string variable, and you change it (such as replacing), it doesnt modify the variable.

    replaceAll returns a string, use that.
     
  3. Offline

    jahoman

    Code:java
    1. killerMessage.replaceAll(killer + " ,you killed someone!");


    Make shure you have Player killer = Player (player) defined and that killer.sendMessage is in the code somewhere.
     
  4. Offline

    CorrieKay

    Uh... Theres a few problems with this.

    1) using "killer" in this way as a Player object, due to how its toString() method works, would format incredibly oddly.
    2) this doesn't compile, the String.replaceAll() method requires two string parameters, and you've only given a single string parameter.
    3) This doesn't even fix his problem, the code would still not modify the killerMessage string.
     
  5. Offline

    567legodude

    CorrieKay Thanks, your solution worked, I just had to change
    Code:java
    1. killerMessage.replaceAll("<killer>", killer);

    with
    Code:java
    1. killerMessage = killerMessage.replaceAll("<killer>", killer);
     
    CorrieKay likes this.
  6. Offline

    CorrieKay

    Exactly :cool:
     
Thread Status:
Not open for further replies.

Share This Page