• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Behaviour of \n(new line ) character

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Paul for your comments .I guess I am now very near to understanding the concept.


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Jeff,
That makes everything crystal clear.
Thank you for the detailed explanation.
I will make necessary arrangements to my code based on your suggestions. :-)
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic