Why does string creation from multiple objects occur after combining the first two objects
I ran into a problem today when I tried to concatenate multiple "variables" or objects, like the following code example:
String stuff = "blah blah" + amazingness.getName() + " fantabulous code: " + address.
The above example works fine fine, but it doesn't work in my case.
So basically I have a server that receives data over UDP. Now, in order to use UDP, I need to create many threads so the application does not hang. I don't know if the problem has something to do with multiple threads, as the processor randomly starts threads in a random order.
I am using java version 1.8 upgrade. 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 the "Client Classic" message and nothing else. Expected message: "Client Classic(1932) @ 70.40.423.110:8112 disconnected"
Here is a screenshot:
Where are the other meanings left?
Now I'm starting to think that it might be because referencing other classes messed up the process of adding more values ββto the string.
So I am trying 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, cID
is a random number, address
is an ip address, and port
is a port number. All variables are not static. Still doesn't output the required message:
NOW, I am testing it again without threads, just a self-contained class:
IT WORKS PERFECTLY! Now, my question is why the above result occurs and what did I do wrong?
I appreciate the time you have taken to read my question and I hope you can figure it out.
Edit 1: I noticed that in the message value the end quotes were never added, will be studied.
Edit 2: It looks like the error is listed in the name variable, although I have no idea why.
Edit 3: It looks like the name variable contains multiple "null" characters, I'll be looking into it.
Edit: I believe I have fixed the problem, I will include a summary:
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.
source to share
Do you only see this behavior in Eclipse? I suspect that what is happening is that your variable name
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 0s with some letters, and then constructs a string similar 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 result of this in eclipse is
Middle
Before Middle
But when I run this in Intellij IDEA I get
Middle
Before Middle After
As expected.
When I add the following code right 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 am getting 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 display problems (eg debugger or even console). SWT uses native widgets (and possibly native C style strings, which means null is complete).
Hope this makes sense - try removing unnecessary lines from your string before printing them, even if they shouldn't be significant in Java.
source to share