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
Solved

adding a null terminator to a java string

Posted on 2012-03-14
6
260 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
  • 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
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.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 104
Java exception bubble up 2 21
spring jars download 1 35
Running JavaFX on JDeveloper 12C 1 52
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

809 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