Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use line break argument [duplicate]

Tags:

java

I wrote the most simple Hello, World! application:

public class Helloworld {

    public static void main(String[] args) {
        System.out.println("Hello\nHello");
    }
}

When it runs, the result is:

Hello
Hello

But if I use Hello\nHello as arguments,

public class Helloworld {

    public static void main(String[] args) {
        System.out.println(args[0]);
    }
}

the result is Hello\nHello. How do I get the two-line result?

like image 956
kk luo Avatar asked Sep 26 '18 11:09

kk luo


People also ask

How do you pass a line break in a string?

Create a string containing line breaksInserting a newline code \n , \r\n into a string will result in a line break at that location. On Unix, including Mac, \n (LF) is often used, and on Windows, \r\n (CR + LF) is often used as a newline code. Some text editors allow you to select a newline code.

How do you do a line break in Python?

Newline character in Python: In Python, the new line character “\n” is used to create a new line. When inserted in a string all the characters after the character are added to a new line.

How do I create a new line as delimiter in Excel?

In the input box to the right of Other press Ctrl + J to insert a line break as your delimiter. You should see dividers appear in the Data preview pane where there are line breaks in your data.

How do you split a single line into multiple lines in Python?

You cannot split a statement into multiple lines in Python by pressing Enter . Instead, use the backslash ( \ ) to indicate that a statement is continued on the next line. In the revised version of the script, a blank space and an underscore indicate that the statement that was started on line 1 is continued on line 2.


2 Answers

EDIT: This answer is for the original question understood as

How come when I write Hello\nHello in Java and then print it, I get two lines of text, but when I write Hello\nHello in my terminal and pass it to my Java program as an argument, I get one line of text?

If you mean

Is there a way to pass one string argument to my java program which gets printed on multiple lines?

see @DodgyCodeException's answer, which is better suited to that formulation.


When you define a String as "Hello\nHello" 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.

like image 83
Alex M Avatar answered Oct 21 '22 19:10

Alex M


If you are using a Unix-like OS such as GNU/Linux or MacOS, or a Bash shell on any other system (such as Cygwin on Windows), then just enclose your command-line argument in single quotes, and you can insert any number of newlines and it will still be treated as a single argument:

$ java Helloworld 'line one
line two'           <-- this is a single argument with an embedded newline

line one            <-- it prints out the output on separate lines!
line two

This will not work on the default Windows Command Processor (cmd.exe). If that case, you might want to use the following technique.

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

like image 9
DodgyCodeException Avatar answered Oct 21 '22 20:10

DodgyCodeException