How to use line break argument

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite
1












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.










share|improve this question



















  • 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














up vote
7
down vote

favorite
1












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.










share|improve this question



















  • 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












up vote
7
down vote

favorite
1









up vote
7
down vote

favorite
1






1





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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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












  • 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







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












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.






share|improve this answer






















  • 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

















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.






share|improve this answer






















  • 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




    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

















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.






share|improve this answer




















    Your Answer





    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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.






    share|improve this answer






















    • 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














    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.






    share|improve this answer






















    • 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












    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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
















    • 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












    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.






    share|improve this answer






















    • 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




      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














    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.






    share|improve this answer






















    • 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




      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












    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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 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




      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
















    • 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




      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















    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










    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.






    share|improve this answer
























      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.






      share|improve this answer






















        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 13 mins ago









        DodgyCodeException

        2,826421




        2,826421



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            White Anglo-Saxon Protestant

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            One-line joke