Solved

adding a null terminator to a java string

Posted on 2012-03-14
6
273 Views
Last Modified: 2012-03-14
I am using a java program to send an xml string to socket being processed by a C program.
When reading the string it is looking for a \n to know the string is complete.
Is the simplest way to do this to add the \n to my java string?
String A = "<test>hello</test>";
String B = (StringA + "\n");
0
Comment
Question by:dpalyca755
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37721108
If it is looking for "\n" then this is indeed the best way to add it
String B = StringA + "\n";
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37721126
End of line can be different on different systems
you can add end of line specific to the system on which you are writing the string in this way:
String B = StringA + System.getProperty("line.separator");

However what it is that your server part is waiting for is another question
0
 
LVL 14

Expert Comment

by:CPColin
ID: 37721150
If you add "\n" you'll be adding a newline character, not a null character. A null character would be something like '\0'
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 37721161
But probably your server will rather be looking for end of line then for null termination
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37721437
You'll find that the System line separator almost certainly does end with \n anyway, in which case you can save yourself and the system the bother of appending \n by using a PrintWriter. Just as a precaution, you can place the following at the head of your app entry point:

        String sep = System.getProperty("line.separator");

        if (!sep.endsWith("\n")) {
            throw new RuntimeException(
                "System line separator not suitable for network app");
        }

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37722049
If it is looking for "\n" then this is indeed the best way to add it
String B = StringA + "\n";

Of all ways to implement it, that's not the best. Quite possibly it's the worst
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question