Solved

adding a null terminator to a java string

Posted on 2012-03-14
6
264 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
Technology Partners: 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

733 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