aspose file tools*
The moose likes Java in General and the fly likes Behaviour of \n(new line ) character Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Behaviour of \n(new line ) character" Watch "Behaviour of \n(new line ) character" New topic
Author

Behaviour of \n(new line ) character

s sarma
Greenhorn

Joined: Sep 24, 2012
Posts: 8
Hi ,
I was working on some problem and came across this issue.

While fetching details from Oracle Db I get a String using resultSet.getString() Method.The String in Db is of the form of "heylo,\nHow are you?".
I am expecting it to print
heylo,
How are you
whereas it is printing the whole String as is i.e. heylo,\nHow are you?.(Not doing a new line after heylo).

When I made a Java program and wrote String s= "heylo,\nHow are you?",it gives a new line after heylo.

a) Can someone explain this behaviour

I searched and found out that /n has some weird behavior for different platforms.
Also new line char differs in various OS. eg it is \r\t in Windows.

I found out that i can use System.getProperty("line.separator") to make new lines independent of platforms.
While debugging I found out that "\" and "n" are being taken as two different characters whereas if I store the String in variable then "\n" is one single character.is it related to difference between encodings of my Java Client and Oracle Db??

b) How do i parse my string coming from Db.Should i replace any occurence of "\n" by System.getProperty("line.separator") using String.replace method ?

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sounds like what got saved to the DB was the '\' character and the 'n' character, rather than the actual newline character.

Your choices are:

1. Save the actual newline character to the DB. (How did that data get in there in the first place?)

or

2. Parse the data coming out of the DB and turn '\' + 'n' into the newline character.

Choice 1 is to be preferred.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18986
    
    8

You are misunderstanding something. When you say "the \n (new line) character" you are missing out on the fact that the new-line character is a single character which is represented by the two characters "\n" in a Java string literal.

If you see those two characters somewhere else -- like in a text field in a database for example -- they simply represent those two characters. And when you read them into a Java string object, they still simply represent those two characters, as you already noticed from your debugging. It's nothing to do with encodings or anything like that, it's just that you have taken a rule which only applies to Java string literals and tried to apply it in other situations.

(You do know what a string literal is, don't you? It's a constant value in your source code which is surrounded by quotes, like "this is a string literal".)
s sarma
Greenhorn

Joined: Sep 24, 2012
Posts: 8
Jeff Verdegan wrote:Sounds like what got saved to the DB was the '\' character and the 'n' character, rather than the actual newline character.

Your choices are:

1. Save the actual newline character to the DB. (How did that data get in there in the first place?)

or

2. Parse the data coming out of the DB and turn '\' + 'n' into the newline character.

Choice 1 is to be preferred.

Jeff,
Thank you for the comments.
Data is coming through a Insert Script . I was inserting String with "\" and "n" and was expecting that it would be recognized by Java as newline characters.
I guess i would be using something like CHR(10) while inserting into DB.I guess that should solve the problem.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18986
    
    8

Yes, if you can do that then it would put the new-line character into the database column, which is what you want.
s sarma
Greenhorn

Joined: Sep 24, 2012
Posts: 8
Thank you Paul for your comments .I guess I am now very near to understanding the concept.


Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

s sarma wrote:
Jeff Verdegan wrote:Sounds like what got saved to the DB was the '\' character and the 'n' character, rather than the actual newline character.

Your choices are:

1. Save the actual newline character to the DB. (How did that data get in there in the first place?)

or

2. Parse the data coming out of the DB and turn '\' + 'n' into the newline character.

Choice 1 is to be preferred.

Jeff,
Thank you for the comments.
Data is coming through a Insert Script . I was inserting String with "\" and "n" and was expecting that it would be recognized by Java as newline characters.


That depends.

If you have


Then, as Paul pointed out, the Java compiler will turn the '\' + 'n' combination in that string literal into the single newline character in your String object.

If, on the other hand, your code does something like prompting the user to input some text, and the user types 'a' then 'b' then 'c' then '\' then 'n', etc., then you will have the two separate '\' and 'n' characters int your String.

There's nothing special about the character sequence '\' 'n' in a Java String object. That's only translated to a newline character in a String literal by the compiler, not in String objects in general.

Now, if you do


Then what goes into the DB and what you get back should include the newline character, since it actually was in your String object.

guess i would be using something like CHR(10) while inserting into DB.I guess that should solve the problem.


If it's coming from Java code, then no, you don't need to do that. If there's some other tool you're using to get or generate the string and then insert it, it depends on that tool's rules for special characters.
s sarma
Greenhorn

Joined: Sep 24, 2012
Posts: 8
@Jeff,
That makes everything crystal clear.
Thank you for the detailed explanation.
I will make necessary arrangements to my code based on your suggestions. :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Behaviour of \n(new line ) character