为什么我在Java中无休止地附加一个String

use*_*891 -5 java

我正在写一个游戏,当我尝试向服务器发送聊天消息时,我得到一个奇怪的内存泄漏.发送字符串:!chat¥Test变为:

!chat¥test
!chat¥!chat¥test
!chat¥!chat¥!chat¥test
!chat¥!chat¥!chat¥!chat¥test
!chat¥!chat¥!chat¥!chat¥!chat¥test
!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥test
!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥test
!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥test
!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥test
!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥test
!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥!chat¥test
Ect, ect.


Sending message:

Utils.sendChat(message.getText());

public static void sendChat(String s) {
    System.out.println(s);
    sendChat("!chat"+Wrapper.commandSplit+s);
}

public static void sendTextLine(String s){
    s = s.replace(" ", "[SPACE]");
    System.out.println(s);
    Wrapper.pw.write(s);
     Wrapper.pw.flush();
}
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会这样,有人能帮帮我吗?我使用'¥'来分割字符串中的变量,之前没有给我任何问题,所以我怀疑它与它有什么关系.

谢谢.

Aff*_*ffe 10

public static void sendChat(String s) {
    System.out.println(s);
    sendChat("!chat"+Wrapper.commandSplit+s);
}
Run Code Online (Sandbox Code Playgroud)

这是一个无限循环,每次你sendChat再次打电话时建立更长更长的琴弦.

你可能打算sendTextLine在那里打电话给第二行?

  • @ user1681891 SO有自己的机制来说"谢谢" - >接受答案.见http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work (4认同)
  • 我希望user1681891回来并将此标记为正确...直到那时才有+1 (2认同)