javastringasciijava-text-blocks

How to output a Multiline Character Image in the console


I am developing a text-based game and I want to make the title something catchy. I tried to use a text to ASCII converter to make a nice-looking title and then copy and paste it in my code to output it, but it didn't work.

Here is what I tried to do:

System.out.println("
██████╗ ██╗   ██╗███╗   ██╗ ██████╗ ███████╗ ██████╗ ███╗   ██╗     ██████╗ ███████╗                            
██╔══██╗██║   ██║████╗  ██║██╔════╝ ██╔════╝██╔═══██╗████╗  ██║    ██╔═══██╗██╔════╝                            
██║  ██║██║   ██║██╔██╗ ██║██║  ███╗█████╗  ██║   ██║██╔██╗ ██║    ██║   ██║█████╗                              
██║  ██║██║   ██║██║╚██╗██║██║   ██║██╔══╝  ██║   ██║██║╚██╗██║    ██║   ██║██╔══╝                              
██████╔╝╚██████╔╝██║ ╚████║╚██████╔╝███████╗╚██████╔╝██║ ╚████║    ╚██████╔╝██║                                 
╚═════╝  ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝ ╚══════╝ ╚═════╝ ╚═╝  ╚═══╝     ╚═════╝ ╚═╝                                 
                                                                                                                
    ████████╗██╗  ██╗███████╗    ███████╗ ██████╗ ██████╗  ██████╗  ██████╗ ████████╗████████╗███████╗███╗   ██╗
    ╚══██╔══╝██║  ██║██╔════╝    ██╔════╝██╔═══██╗██╔══██╗██╔════╝ ██╔═══██╗╚══██╔══╝╚══██╔══╝██╔════╝████╗  ██║
       ██║   ███████║█████╗      █████╗  ██║   ██║██████╔╝██║  ███╗██║   ██║   ██║      ██║   █████╗  ██╔██╗ ██║
       ██║   ██╔══██║██╔══╝      ██╔══╝  ██║   ██║██╔══██╗██║   ██║██║   ██║   ██║      ██║   ██╔══╝  ██║╚██╗██║
       ██║   ██║  ██║███████╗    ██║     ╚██████╔╝██║  ██║╚██████╔╝╚██████╔╝   ██║      ██║   ███████╗██║ ╚████║
       ╚═╝   ╚═╝  ╚═╝╚══════╝    ╚═╝      ╚═════╝ ╚═╝  ╚═╝ ╚═════╝  ╚═════╝    ╚═╝      ╚═╝   ╚══════╝╚═╝  ╚═══╝");

But it didn't seem to work. I know it works in JavaScript, but I was wondering if it would also work in Java.


Solution

  • String literals

    String literal - is a string consists of zero or more characters enclosed in double quotes "myText".

    It is not possible to create a multiline string literal in Java, precisely as you've tried it. According to the language specification it's a compile-time error, line termination is present in a string lateral.

    String invalidLiteral = "line1
                             line2
                             ..."; // will cause a compilation error
    

    To make such string literal compile, line termination can be replaced with a new line character \n.

    String validString = "line1\nline2\nline3";
    

    The resulting string can become very long, in order to make it readable you can split the string into chunks concatenated with a plus sign +, appending a new line character \n to each chunk.

    String validConcatinatedString = "line1\n" +
                                     "line2\n" +
                                     "line3";
    

    Although it'll work, it might be tedious.

    Text Blocks

    With Java 15 you can create a multiline string using text blocks available.

    In order to create a text block, you need to enclose the target multiline text in triple double-quote characters """.

    String myASCIIArt = """ // no text after the opening delimiter
                  Your amazing 
                      ASCII art 
                           here""";
    

    Note that the opening delimiter """ should be immediately followed by the line termination, and the actual body of the text block always starts on the next line.