• Post Reply Bookmark Topic Watch Topic
  • New Topic

New Line \n Issue in the argument passed to java.  RSS feed

 
Purvesh Vora
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a simple java program which takes some arguments and prints. I am passing arguments as shown below.

java -jar testemail.jar -BODY "EMAIL WITH ATTACHMENT\nRegards,\nPurvesh"

In the public static void main method I am reading the argument and printing it.

But it is NOT printing new line. The \n provided in the argument is replaced by \\n.

How do I retain the \n character?

Thanks in advance


Regards,
Purvesh
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The commandline you show does NOT contain a newline character. It has the two characters \n in it. A newline character has the hex value (of 0x0D?).
In may programming languages the compiler supports various ways of having characters in strings that can't be entered from the keyboard. The escape sequence \x is how java does it.
If you want your program to handle the two characters \n as a newline, you'll have to write your own code for it.

>java -jar testemail.jar -BODY "EMAIL WITH ATTACHMENT\nRegards,\nPurvesh"
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using Unix, you can just type the Enter key. Since there is an open quote, the shell will give you the "secondary command prompt" and you can continue entering text. The shell will not execute the command until all open quotes have a matching close quote.

I don't know if Windows provides a similar feature or not.

Alternatively, you can use String.replaceAll() to replace the "\\n" with a new line character.

Layne
[ September 25, 2005: Message edited by: Layne Lund ]
 
Purvesh Vora
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Norm & Layne for your reply.

Actually I was calling email jar from the unix shell script. And in the message body for new line I was putting \n.
But when it was passed as an arg to the program it was replacing \n with \\n.

As suggested by Layne I tried string.replaceAll and it works perfectly

Regards,
Purvesh
[ September 23, 2005: Message edited by: Purvesh Vora ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!