How to use line break argument

Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
I wrote the most simple helloworld.
public class Helloworld
public static void main(String args)
System.out.println("HellonHello");
when it run ,the result is
Hello
Hello
But if I use HellonHello as arguments
public class Helloworld
public static void main(String args)
System.out.println(args[0]);
the result is HellonHello.How to get the two line result? Thx.
java
add a comment |Â
up vote
7
down vote
favorite
I wrote the most simple helloworld.
public class Helloworld
public static void main(String args)
System.out.println("HellonHello");
when it run ,the result is
Hello
Hello
But if I use HellonHello as arguments
public class Helloworld
public static void main(String args)
System.out.println(args[0]);
the result is HellonHello.How to get the two line result? Thx.
java
2
Possible duplicate of Java String new line
â Brank Victoria
1 hour ago
4
@BrankVictoria I don't think that is the problem here - the problem is how the command line argument arrives in java
â Hulk
1 hour ago
2
@Hulk you're right I miss understood the question. It is really possible that the console formats the string. I mean that if you input "HellonHello" what does args[0] contains would be "Hello\nHello"
â Brank Victoria
1 hour ago
You know, now I'm curious... I never needed something like that... Usually, when I needed new lines as arguments, I used a file input with all properties I need...
â Leonardo Alves Machado
1 hour ago
If you are using Linux or MacOS, or the Bash shell on any system, then you can usejava Helloworld 'line one<Enter>line two'where<Enter>means you press the Enter key so that the two lines appear on separate lines. The single quotes keep them as a single argument.
â DodgyCodeException
56 mins ago
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
I wrote the most simple helloworld.
public class Helloworld
public static void main(String args)
System.out.println("HellonHello");
when it run ,the result is
Hello
Hello
But if I use HellonHello as arguments
public class Helloworld
public static void main(String args)
System.out.println(args[0]);
the result is HellonHello.How to get the two line result? Thx.
java
I wrote the most simple helloworld.
public class Helloworld
public static void main(String args)
System.out.println("HellonHello");
when it run ,the result is
Hello
Hello
But if I use HellonHello as arguments
public class Helloworld
public static void main(String args)
System.out.println(args[0]);
the result is HellonHello.How to get the two line result? Thx.
java
java
edited 1 hour ago
Hulk
2,14411432
2,14411432
asked 1 hour ago
kk luo
764
764
2
Possible duplicate of Java String new line
â Brank Victoria
1 hour ago
4
@BrankVictoria I don't think that is the problem here - the problem is how the command line argument arrives in java
â Hulk
1 hour ago
2
@Hulk you're right I miss understood the question. It is really possible that the console formats the string. I mean that if you input "HellonHello" what does args[0] contains would be "Hello\nHello"
â Brank Victoria
1 hour ago
You know, now I'm curious... I never needed something like that... Usually, when I needed new lines as arguments, I used a file input with all properties I need...
â Leonardo Alves Machado
1 hour ago
If you are using Linux or MacOS, or the Bash shell on any system, then you can usejava Helloworld 'line one<Enter>line two'where<Enter>means you press the Enter key so that the two lines appear on separate lines. The single quotes keep them as a single argument.
â DodgyCodeException
56 mins ago
add a comment |Â
2
Possible duplicate of Java String new line
â Brank Victoria
1 hour ago
4
@BrankVictoria I don't think that is the problem here - the problem is how the command line argument arrives in java
â Hulk
1 hour ago
2
@Hulk you're right I miss understood the question. It is really possible that the console formats the string. I mean that if you input "HellonHello" what does args[0] contains would be "Hello\nHello"
â Brank Victoria
1 hour ago
You know, now I'm curious... I never needed something like that... Usually, when I needed new lines as arguments, I used a file input with all properties I need...
â Leonardo Alves Machado
1 hour ago
If you are using Linux or MacOS, or the Bash shell on any system, then you can usejava Helloworld 'line one<Enter>line two'where<Enter>means you press the Enter key so that the two lines appear on separate lines. The single quotes keep them as a single argument.
â DodgyCodeException
56 mins ago
2
2
Possible duplicate of Java String new line
â Brank Victoria
1 hour ago
Possible duplicate of Java String new line
â Brank Victoria
1 hour ago
4
4
@BrankVictoria I don't think that is the problem here - the problem is how the command line argument arrives in java
â Hulk
1 hour ago
@BrankVictoria I don't think that is the problem here - the problem is how the command line argument arrives in java
â Hulk
1 hour ago
2
2
@Hulk you're right I miss understood the question. It is really possible that the console formats the string. I mean that if you input "HellonHello" what does args[0] contains would be "Hello\nHello"
â Brank Victoria
1 hour ago
@Hulk you're right I miss understood the question. It is really possible that the console formats the string. I mean that if you input "HellonHello" what does args[0] contains would be "Hello\nHello"
â Brank Victoria
1 hour ago
You know, now I'm curious... I never needed something like that... Usually, when I needed new lines as arguments, I used a file input with all properties I need...
â Leonardo Alves Machado
1 hour ago
You know, now I'm curious... I never needed something like that... Usually, when I needed new lines as arguments, I used a file input with all properties I need...
â Leonardo Alves Machado
1 hour ago
If you are using Linux or MacOS, or the Bash shell on any system, then you can use
java Helloworld 'line one<Enter>line two' where <Enter> means you press the Enter key so that the two lines appear on separate lines. The single quotes keep them as a single argument.â DodgyCodeException
56 mins ago
If you are using Linux or MacOS, or the Bash shell on any system, then you can use
java Helloworld 'line one<Enter>line two' where <Enter> means you press the Enter key so that the two lines appear on separate lines. The single quotes keep them as a single argument.â DodgyCodeException
56 mins ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
8
down vote
When you define a String as "HellonHello" in Java, it contains no '' character. It is an escape sequence for the line break: "n" is just one character.
When you use this string as an argument to your program, however (so the string is defined outside), "n" is interpreted as two characters: '' and 'n'. You have to replace these two characters with the line break, knowing that to match '' you have to escape it with '':
System.out.println(args[0].replace("\n", "n"));
For portability concerns, you can also use System.lineSeparator() as the replacement string.
Good answer, but note that your proposed solution means the user will never again be able to pass the two characters "n" as an argument.
â DodgyCodeException
59 mins ago
1
Well, if your arguments contains"n"'s that should be interpreted as line breaks and"n"'s that should remain as two characters, then you should find a better solution. Namely, using more than one argument, or defining the string in Java.
â Alex M
46 mins ago
add a comment |Â
up vote
2
down vote
Apparently you can't do it straight away but doing the following trick will get you what you need:
System.out.println(String.format(args[0]));
Here String#format is used and the new line is passed as the conversion sequence for a new line. (see the format string syntax).
So call your program with
java Helloworld "hello%nnworld"
will print
hello
workd
and if you need to output %n itself then you can quote '%' with another '%' i.e.
java Helloworld "hello%%nnworld"
The above will print:
hello%nnworld
Btw,
System.out.printf("%s%n", args[0]);
will do the same. DodgyCodeException is right, it will not print the same, the string would still need to be passed to String#format.
I was first thinking about the same answer as you, but there is an unwanted side effect: if someone calls the program withHello%sHelloor similar, the program will throw an exception. I think%nis better thannbut I do not agree to let the user to type in the input forprintf.
â Honza Zidek
41 mins ago
1
System.out.printf("%s%n", args[0]);prints the command-line argument verbatim (i.e. it will print "Hello%nworld"). Presumably you meantSystem.out.printf(args[0]);.
â DodgyCodeException
39 mins ago
yes, because it is interpreted as format and String.format will expect an argument if it encounters an formatting sequence. Actually in this case arguments should passed as file or making the app read from stdin.
â A4L
39 mins ago
add a comment |Â
up vote
1
down vote
You could use the StringEscapeUtils.unescapeJava method from Apache Commons Text. Then you will be able to pass command-line arguments and have them interpreted exactly[*] like a literal string in source code:
import static apache.commons.text.StringEscapeUtils.unescapeJava;
public class Helloworld
public static void main(String args)
System.out.println(unescapeJava(args[0]));
[*] Barring any remaining bugs in the Apache method.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
When you define a String as "HellonHello" in Java, it contains no '' character. It is an escape sequence for the line break: "n" is just one character.
When you use this string as an argument to your program, however (so the string is defined outside), "n" is interpreted as two characters: '' and 'n'. You have to replace these two characters with the line break, knowing that to match '' you have to escape it with '':
System.out.println(args[0].replace("\n", "n"));
For portability concerns, you can also use System.lineSeparator() as the replacement string.
Good answer, but note that your proposed solution means the user will never again be able to pass the two characters "n" as an argument.
â DodgyCodeException
59 mins ago
1
Well, if your arguments contains"n"'s that should be interpreted as line breaks and"n"'s that should remain as two characters, then you should find a better solution. Namely, using more than one argument, or defining the string in Java.
â Alex M
46 mins ago
add a comment |Â
up vote
8
down vote
When you define a String as "HellonHello" in Java, it contains no '' character. It is an escape sequence for the line break: "n" is just one character.
When you use this string as an argument to your program, however (so the string is defined outside), "n" is interpreted as two characters: '' and 'n'. You have to replace these two characters with the line break, knowing that to match '' you have to escape it with '':
System.out.println(args[0].replace("\n", "n"));
For portability concerns, you can also use System.lineSeparator() as the replacement string.
Good answer, but note that your proposed solution means the user will never again be able to pass the two characters "n" as an argument.
â DodgyCodeException
59 mins ago
1
Well, if your arguments contains"n"'s that should be interpreted as line breaks and"n"'s that should remain as two characters, then you should find a better solution. Namely, using more than one argument, or defining the string in Java.
â Alex M
46 mins ago
add a comment |Â
up vote
8
down vote
up vote
8
down vote
When you define a String as "HellonHello" in Java, it contains no '' character. It is an escape sequence for the line break: "n" is just one character.
When you use this string as an argument to your program, however (so the string is defined outside), "n" is interpreted as two characters: '' and 'n'. You have to replace these two characters with the line break, knowing that to match '' you have to escape it with '':
System.out.println(args[0].replace("\n", "n"));
For portability concerns, you can also use System.lineSeparator() as the replacement string.
When you define a String as "HellonHello" in Java, it contains no '' character. It is an escape sequence for the line break: "n" is just one character.
When you use this string as an argument to your program, however (so the string is defined outside), "n" is interpreted as two characters: '' and 'n'. You have to replace these two characters with the line break, knowing that to match '' you have to escape it with '':
System.out.println(args[0].replace("\n", "n"));
For portability concerns, you can also use System.lineSeparator() as the replacement string.
edited 11 mins ago
answered 1 hour ago
Alex M
3939
3939
Good answer, but note that your proposed solution means the user will never again be able to pass the two characters "n" as an argument.
â DodgyCodeException
59 mins ago
1
Well, if your arguments contains"n"'s that should be interpreted as line breaks and"n"'s that should remain as two characters, then you should find a better solution. Namely, using more than one argument, or defining the string in Java.
â Alex M
46 mins ago
add a comment |Â
Good answer, but note that your proposed solution means the user will never again be able to pass the two characters "n" as an argument.
â DodgyCodeException
59 mins ago
1
Well, if your arguments contains"n"'s that should be interpreted as line breaks and"n"'s that should remain as two characters, then you should find a better solution. Namely, using more than one argument, or defining the string in Java.
â Alex M
46 mins ago
Good answer, but note that your proposed solution means the user will never again be able to pass the two characters "n" as an argument.
â DodgyCodeException
59 mins ago
Good answer, but note that your proposed solution means the user will never again be able to pass the two characters "n" as an argument.
â DodgyCodeException
59 mins ago
1
1
Well, if your arguments contains
"n"'s that should be interpreted as line breaks and "n"'s that should remain as two characters, then you should find a better solution. Namely, using more than one argument, or defining the string in Java.â Alex M
46 mins ago
Well, if your arguments contains
"n"'s that should be interpreted as line breaks and "n"'s that should remain as two characters, then you should find a better solution. Namely, using more than one argument, or defining the string in Java.â Alex M
46 mins ago
add a comment |Â
up vote
2
down vote
Apparently you can't do it straight away but doing the following trick will get you what you need:
System.out.println(String.format(args[0]));
Here String#format is used and the new line is passed as the conversion sequence for a new line. (see the format string syntax).
So call your program with
java Helloworld "hello%nnworld"
will print
hello
workd
and if you need to output %n itself then you can quote '%' with another '%' i.e.
java Helloworld "hello%%nnworld"
The above will print:
hello%nnworld
Btw,
System.out.printf("%s%n", args[0]);
will do the same. DodgyCodeException is right, it will not print the same, the string would still need to be passed to String#format.
I was first thinking about the same answer as you, but there is an unwanted side effect: if someone calls the program withHello%sHelloor similar, the program will throw an exception. I think%nis better thannbut I do not agree to let the user to type in the input forprintf.
â Honza Zidek
41 mins ago
1
System.out.printf("%s%n", args[0]);prints the command-line argument verbatim (i.e. it will print "Hello%nworld"). Presumably you meantSystem.out.printf(args[0]);.
â DodgyCodeException
39 mins ago
yes, because it is interpreted as format and String.format will expect an argument if it encounters an formatting sequence. Actually in this case arguments should passed as file or making the app read from stdin.
â A4L
39 mins ago
add a comment |Â
up vote
2
down vote
Apparently you can't do it straight away but doing the following trick will get you what you need:
System.out.println(String.format(args[0]));
Here String#format is used and the new line is passed as the conversion sequence for a new line. (see the format string syntax).
So call your program with
java Helloworld "hello%nnworld"
will print
hello
workd
and if you need to output %n itself then you can quote '%' with another '%' i.e.
java Helloworld "hello%%nnworld"
The above will print:
hello%nnworld
Btw,
System.out.printf("%s%n", args[0]);
will do the same. DodgyCodeException is right, it will not print the same, the string would still need to be passed to String#format.
I was first thinking about the same answer as you, but there is an unwanted side effect: if someone calls the program withHello%sHelloor similar, the program will throw an exception. I think%nis better thannbut I do not agree to let the user to type in the input forprintf.
â Honza Zidek
41 mins ago
1
System.out.printf("%s%n", args[0]);prints the command-line argument verbatim (i.e. it will print "Hello%nworld"). Presumably you meantSystem.out.printf(args[0]);.
â DodgyCodeException
39 mins ago
yes, because it is interpreted as format and String.format will expect an argument if it encounters an formatting sequence. Actually in this case arguments should passed as file or making the app read from stdin.
â A4L
39 mins ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Apparently you can't do it straight away but doing the following trick will get you what you need:
System.out.println(String.format(args[0]));
Here String#format is used and the new line is passed as the conversion sequence for a new line. (see the format string syntax).
So call your program with
java Helloworld "hello%nnworld"
will print
hello
workd
and if you need to output %n itself then you can quote '%' with another '%' i.e.
java Helloworld "hello%%nnworld"
The above will print:
hello%nnworld
Btw,
System.out.printf("%s%n", args[0]);
will do the same. DodgyCodeException is right, it will not print the same, the string would still need to be passed to String#format.
Apparently you can't do it straight away but doing the following trick will get you what you need:
System.out.println(String.format(args[0]));
Here String#format is used and the new line is passed as the conversion sequence for a new line. (see the format string syntax).
So call your program with
java Helloworld "hello%nnworld"
will print
hello
workd
and if you need to output %n itself then you can quote '%' with another '%' i.e.
java Helloworld "hello%%nnworld"
The above will print:
hello%nnworld
Btw,
System.out.printf("%s%n", args[0]);
will do the same. DodgyCodeException is right, it will not print the same, the string would still need to be passed to String#format.
edited 35 mins ago
answered 44 mins ago
A4L
14.5k43151
14.5k43151
I was first thinking about the same answer as you, but there is an unwanted side effect: if someone calls the program withHello%sHelloor similar, the program will throw an exception. I think%nis better thannbut I do not agree to let the user to type in the input forprintf.
â Honza Zidek
41 mins ago
1
System.out.printf("%s%n", args[0]);prints the command-line argument verbatim (i.e. it will print "Hello%nworld"). Presumably you meantSystem.out.printf(args[0]);.
â DodgyCodeException
39 mins ago
yes, because it is interpreted as format and String.format will expect an argument if it encounters an formatting sequence. Actually in this case arguments should passed as file or making the app read from stdin.
â A4L
39 mins ago
add a comment |Â
I was first thinking about the same answer as you, but there is an unwanted side effect: if someone calls the program withHello%sHelloor similar, the program will throw an exception. I think%nis better thannbut I do not agree to let the user to type in the input forprintf.
â Honza Zidek
41 mins ago
1
System.out.printf("%s%n", args[0]);prints the command-line argument verbatim (i.e. it will print "Hello%nworld"). Presumably you meantSystem.out.printf(args[0]);.
â DodgyCodeException
39 mins ago
yes, because it is interpreted as format and String.format will expect an argument if it encounters an formatting sequence. Actually in this case arguments should passed as file or making the app read from stdin.
â A4L
39 mins ago
I was first thinking about the same answer as you, but there is an unwanted side effect: if someone calls the program with
Hello%sHello or similar, the program will throw an exception. I think %n is better than n but I do not agree to let the user to type in the input for printf.â Honza Zidek
41 mins ago
I was first thinking about the same answer as you, but there is an unwanted side effect: if someone calls the program with
Hello%sHello or similar, the program will throw an exception. I think %n is better than n but I do not agree to let the user to type in the input for printf.â Honza Zidek
41 mins ago
1
1
System.out.printf("%s%n", args[0]); prints the command-line argument verbatim (i.e. it will print "Hello%nworld"). Presumably you meant System.out.printf(args[0]);.â DodgyCodeException
39 mins ago
System.out.printf("%s%n", args[0]); prints the command-line argument verbatim (i.e. it will print "Hello%nworld"). Presumably you meant System.out.printf(args[0]);.â DodgyCodeException
39 mins ago
yes, because it is interpreted as format and String.format will expect an argument if it encounters an formatting sequence. Actually in this case arguments should passed as file or making the app read from stdin.
â A4L
39 mins ago
yes, because it is interpreted as format and String.format will expect an argument if it encounters an formatting sequence. Actually in this case arguments should passed as file or making the app read from stdin.
â A4L
39 mins ago
add a comment |Â
up vote
1
down vote
You could use the StringEscapeUtils.unescapeJava method from Apache Commons Text. Then you will be able to pass command-line arguments and have them interpreted exactly[*] like a literal string in source code:
import static apache.commons.text.StringEscapeUtils.unescapeJava;
public class Helloworld
public static void main(String args)
System.out.println(unescapeJava(args[0]));
[*] Barring any remaining bugs in the Apache method.
add a comment |Â
up vote
1
down vote
You could use the StringEscapeUtils.unescapeJava method from Apache Commons Text. Then you will be able to pass command-line arguments and have them interpreted exactly[*] like a literal string in source code:
import static apache.commons.text.StringEscapeUtils.unescapeJava;
public class Helloworld
public static void main(String args)
System.out.println(unescapeJava(args[0]));
[*] Barring any remaining bugs in the Apache method.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You could use the StringEscapeUtils.unescapeJava method from Apache Commons Text. Then you will be able to pass command-line arguments and have them interpreted exactly[*] like a literal string in source code:
import static apache.commons.text.StringEscapeUtils.unescapeJava;
public class Helloworld
public static void main(String args)
System.out.println(unescapeJava(args[0]));
[*] Barring any remaining bugs in the Apache method.
You could use the StringEscapeUtils.unescapeJava method from Apache Commons Text. Then you will be able to pass command-line arguments and have them interpreted exactly[*] like a literal string in source code:
import static apache.commons.text.StringEscapeUtils.unescapeJava;
public class Helloworld
public static void main(String args)
System.out.println(unescapeJava(args[0]));
[*] Barring any remaining bugs in the Apache method.
answered 13 mins ago
DodgyCodeException
2,826421
2,826421
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52517213%2fhow-to-use-line-break-argument%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password

2
Possible duplicate of Java String new line
â Brank Victoria
1 hour ago
4
@BrankVictoria I don't think that is the problem here - the problem is how the command line argument arrives in java
â Hulk
1 hour ago
2
@Hulk you're right I miss understood the question. It is really possible that the console formats the string. I mean that if you input "HellonHello" what does args[0] contains would be "Hello\nHello"
â Brank Victoria
1 hour ago
You know, now I'm curious... I never needed something like that... Usually, when I needed new lines as arguments, I used a file input with all properties I need...
â Leonardo Alves Machado
1 hour ago
If you are using Linux or MacOS, or the Bash shell on any system, then you can use
java Helloworld 'line one<Enter>line two'where<Enter>means you press the Enter key so that the two lines appear on separate lines. The single quotes keep them as a single argument.â DodgyCodeException
56 mins ago