为什么在连接前两个对象后,从多个对象创建字符串失败

时间:2014-08-07 03:19:27

标签: java multithreading

今天,当我尝试组合多个"变量时,我遇到了一个问题。或对象,如下面的示例代码:

String stuff = "blah blah" + amazingness.getName() + " fantabulous code: " + address.

以上示例正常工作正常,但在我的情况下不起作用。

基本上,我有一台通过UDP方式接收数据的服务器。现在,为了使用UDP,我必须创建许多线程,因此应用程序不会挂起。我不知道问题是否与多线程有关,因为处理器以随机顺序随机运行线程。

我正在使用java版本1.8更新05。 这是我方法中的代码。

String message = "Client" + c.name + "(" + c.getID() + ") @" + c.address.toString() + ":" + c.port + "disconnected";

其中:c.name是" Classic",c.getID()返回一个随机数,c.address.toString()返回客户端的IP地址,c.port是客户端的端口。

当我运行代码时,我收到一条消息" Client Classic"没有别的。 预期的消息是:"Client Classic(1932) @ 70.40.423.110:8112 disconnected" 这是一个屏幕截图: Screen shot of inline message

其他价值何去何从?

现在我开始认为这可能是因为引用其他类会混淆向字符串添加更多值的过程。

所以我在设置消息字符串

之前尝试获取值
String message = "";
    String name = c.name;
    int cID = c.getID();
    String address = c.address.toString();
    int port = c.port;
    message = "Client " + name + "(" + cID + ") @ " + address + ":" + port + "disconnected";

    System.out.println(message);

其中:String name为Classic,cID为随机数,address为IP地址,port为端口号。所有变量都不是静态的。仍然没有输出所需的消息: Image showing the value of the message string

现在,我在没有线程的情况下再次测试它,只是一个独立的类: enter image description here

完美的工作!现在,我的问题是为什么上述结果会发生,我做错了什么?

我感谢你花时间阅读我的问题,我希望你能理解它。

编辑1:我注意到,在消息的值中,最终的引用从未被添加,将会查看它。

编辑2:看起来故障在名称变量中,虽然我不知道为什么。

编辑3:看起来名称变量包含多个" null"人物,我会调查一下。

编辑:我相信我已经解决了问题,我将包含一个纲要:

  Since I created a byte array of length 1024 to send through the network, I never trimmed the array before sending.
  Calling "string.trim()" deletes the excess whitespace characters created when I set the variable "name".
  Even though I had found this out without seeing the answer from David, I'll credit him with the correct answer.

2 个答案:

答案 0 :(得分:4)

您是否只在Eclipse中看到此行为?我怀疑发生的事情是你的name变量包含一个null(0)字符,这对Java本身来说不是问题,但可能是SWT(Eclipse使用的widget工具包)的问题。 / p>

例如,下面的代码创建一个用零填充的数组,用一些字母覆盖前几个0,然后构造一个类似于你的例子的字符串。

public class TestMain {

    public static void main(String args[]) {
        byte[] badChars = new byte[10]; // all bytes are 0
        String test = "Middle";
        for (int i = 0;i < test.length() && i < badChars.length; ++i) {
            badChars[i] = (byte) test.charAt(i);
        }        
        String a = new String(badChars);        
        System.out.println(a);        
        String p = "Before " + new String(badChars) + " After";
        System.out.println(p);
    }
}

日食中的输出是

Middle
Before Middle

但是当我在Intellij IDEA中运行时,我得到了

Middle
Before Middle After

正如您所期望的那样。

当我在上面的最后一个println之后添加以下代码时:

StringBuffer b = new StringBuffer();
for (int i = 0; i < p.length(); ++i) {
    char ch = p.charAt(i);
    if (ch > 0) {
        b.append(ch);
    }
}

System.out.println(b.toString());

我在Eclipse中得到以下内容

Middle
Before Middle
Before Middle After

所以我认为你的'name'可能包含一个null char,这会在显示时引起SWT问题(例如调试器,甚至是控制台)。 SWT使用本机小部件(可能是本机C样式字符串,意味着空终止)。

希望这是有道理的 - 尝试在打印之前从字符串中删除空值,即使它们在Java中不重要。

答案 1 :(得分:0)

你可以尝试:

String message = String.format("Client %s (%d) @ %s:%d disconnected", name, cID, address, port);