Today, I encountered a problem when I tried to combine multiple "variables" or objects, like the following example code:
String stuff = "blah blah" + amazingness.getName() + " fantabulous code: " + address.
The above example works fine normally, but it does not work in my case.
So basically, I have a server which receives data via UDP means. Now, in order to use UDP, I have to create many threads, so the application does not hang. I don't know if the problem has anything to do with multiple threading, since the processor randomly runs the threads in a random order.
I am using java version 1.8 update 05. Here is the code in my method.
String message = "Client" + c.name + "(" + c.getID() + ") @" + c.address.toString() + ":" + c.port + "disconnected";
Where:c.name is "Classic", c.getID() returns a random number, c.address.toString() returns the client's ip address, and c.port is the client's port.
When I run the code, I get a message of "Client Classic" and nothing else.
The expected message is: "Client Classic(1932) @ 70.40.423.110:8112 disconnected"
Here is a screen shot:

Where has the other values gone?
Now I start to think it may be because referencing other classes messes up the process of adding more values to the string.
So I try to get the values before setting the message string
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);
Where:String name is Classic, cIDis a random number, address is an ip address, and port is a port number. All of the variables are not static. Still does not output the desired message:

NOW, I test it again without the threads, just a standalone class:

IT WORKS PERFECTLY! Now, my question is why does the above result occur and what did I do wrong?
I appreciate the time you have spent reading my question and I hope you can understand it.
Edit 1: I noticed, in the value of message, the end quotations were never added, will look into it.
Edit 2: looks like the fault is in the name variable, although I have no idea why.
Edit 3: Looks like the name variable contains multiple "null" characters, I will look into it.
Edit: I believe I have fixed the problem, I'll contain a rundown:
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.
Do you only see this behavior in Eclipse? I suspect that what is happening is that your name variable contains a null (0) character, which is not a problem for Java per se, but may be a problem for SWT (the widget toolkit used by Eclipse).
For example, the following code creates an array filled with zeros, overwrites the first few 0's with some letters, and then constructs a string similarly to your example.
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);
}
}
The output of this in eclipse is
Middle
Before Middle
But when I run this within Intellij IDEA, I get
Middle
Before Middle After
Just as you would expect.
When I add the following code just after the last println above:
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());
I get the following in Eclipse
Middle
Before Middle
Before Middle After
So I think it is possible that your 'name' contains a null char, which is causing SWT problems when displayed (such as the debugger, or even the console). SWT uses native widgets (and probably native C style strings, meaning null terminated).
Hope this makes sense - try stripping nulls from your string before you print them, even though they shouldn't be significant in Java.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With