Solved

adding a null terminator to a java string

Posted on 2012-03-14
6
257 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mockito example issue 8 73
Spring Framework HTTPSession management 1 36
hibernate example for saving data 19 42
Java Restore security prompts not working 10 13
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

810 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