Jacob072
Jacob072

Reputation: 1

Printing elements of the array on the same line, when a single element uses "\n" replacement to draw a letter

I found an assignment where I am supposed to print user input as big letters written with '#'. As in user input: "ta" output:

##########      #     
    #          # #    
    #         #   #   
    #        #######  
    #        #     #  
    #       #       # 

I've written below code and it works fine however it prints a everything on a new line due to system print line I am replacing "," with "\n" so obviously it goes to a new line in order to draw the letter properly, and there is a system print in the for loop ( instead of system print ) so it would look proper when printing the letters under each other. However is there a way to print the letters one after another rather than under each other? If I am replacing "," with /n can I get back to the first "print line" before printing element [1] instead of [0]?

Any suggestions would be appreciated.

import java.util.Arrays; import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    System.out.println("Please input a sentence you would like to convert: ");
    Scanner sc = new Scanner(System.in);

    String sentence = sc.next();
    String alphabet = "abcdefghijklmonpqrstuvwxyz";

    char[] sentenceArray;

    sentenceArray = sentence.toCharArray();

    for (int i = 0; i < sentenceArray.length; i++) {
        for (int j = 0; j < alphabet.length(); j++) {
            if (sentenceArray[i] == alphabet.charAt(j)) {
                System.out.println(Arrays.toString(letters[j]).replace(",", "\n").replace("[", "")
                        .replace("]", ""));
            }
        }
    }


}

private static String[][] letters =
        {
                {
                        "    #     ",
                        "   # #    ",
                        "  #   #   ",
                        " #######  ",
                        " #     #  ",
                        "#       # "
                },
                {
                        "#######   ",
                        "#      #  ",
                        "#######   ",
                        "#       # ",
                        "#       # ",
                        "#######   "
                },
                {
                        "  ######  ",
                        " #      # ",
                        "#         ",
                        "#         ",
                        "#       # ",
                        "  #####   "
                },
                {
                        "#######   ",
                        "#      #  ",
                        "#       # ",
                        "#       # ",
                        "#      #  ",
                        "######    "
                },
                {
                        "######### ",
                        "#         ",
                        "########  ",
                        "#         ",
                        "#         ",
                        "######### "
                },
                {
                        "######### ",
                        "#         ",
                        "########  ",
                        "#         ",
                        "#         ",
                        "#         "
                },
                {
                        "  ######  ",
                        " #      # ",
                        "#         ",
                        "#  ###### ",
                        "#       # ",
                        "  #####   "
                },
                {
                        "#       # ",
                        "#       # ",
                        "######### ",
                        "#       # ",
                        "#       # ",
                        "#       # "
                },
                {
                        "   ###    ",
                        "    #     ",
                        "    #     ",
                        "    #     ",
                        "    #     ",
                        "   ###    "
                },
                {
                        "    ##### ",
                        "       #  ",
                        "       #  ",
                        "       #  ",
                        " #     #  ",
                        "   ###    "
                },
                {
                        "#       # ",
                        "#     #   ",
                        "#####     ",
                        "#     #   ",
                        "#      #  ",
                        "#       # "
                },
                {
                        "#         ",
                        "#         ",
                        "#         ",
                        "#         ",
                        "#         ",
                        "######### "
                },
                {
                        "#       # ",
                        "# #   # # ",
                        "#   #   # ",
                        "#       # ",
                        "#       # ",
                        "#       # "
                },
                {
                        "#       # ",
                        "# #     # ",
                        "#  #    # ",
                        "#    #  # ",
                        "#     # # ",
                        "#       # "
                },
                {
                        "  ######  ",
                        " #      # ",
                        "#       # ",
                        "#       # ",
                        "#      #  ",
                        "  ####    "
                },
                {
                        "########  ",
                        "#       # ",
                        "#######   ",
                        "#         ",
                        "#         ",
                        "#         "
                },
                {
                        "  ######  ",
                        " #      # ",
                        "#       # ",
                        "#       # ",
                        "#     ##  ",
                        "  ####  # "
                },
                {
                        "########  ",
                        "#       # ",
                        "#######   ",
                        "#     #   ",
                        "#      #  ",
                        "#       # "
                },
                {
                        "  ######  ",
                        "#         ",
                        "  ######  ",
                        "        # ",
                        "#       # ",
                        "  #####   "
                },
                {
                        "##########",
                        "    #     ",
                        "    #     ",
                        "    #     ",
                        "    #     ",
                        "    #     "
                },
                {
                        "#       # ",
                        "#       # ",
                        "#       # ",
                        "#       # ",
                        " #     #  ",
                        "   ###    "
                },
                {
                        "#       # ",
                        "#       # ",
                        " #     #  ",
                        "  #   #   ",
                        "   # #    ",
                        "    #     "
                },
                {
                        "#       # ",
                        "#       # ",
                        "#       # ",
                        "#   #   # ",
                        " # # # #  ",
                        "  #   #   "
                },
                {
                        "#       # ",
                        "  #   #   ",
                        "    #     ",
                        "  #   #   ",
                        " #     #  ",
                        "#       # "
                },
                {
                        "#       # ",
                        "  #   #   ",
                        "    #     ",
                        "    #     ",
                        "    #     ",
                        "    #     "
                },
                {
                        "######### ",
                        "      #   ",
                        "    #     ",
                        "  #       ",
                        " #        ",
                        "######### "
                }

        };

}

Upvotes: 0

Views: 132

Answers (2)

Simon Crane
Simon Crane

Reputation: 2182

Work out everything you need to print before printing anything, then you can print the top of all letters in the first row, then the second of all letters at once etc.

        sentenceArray = sentence.toCharArray();
        String[] rowsToPrint = new String[6];
        for (int i = 0; i < 6; i++) {
            rowsToPrint[i] = "";
        }

        for (int i = 0; i < sentenceArray.length; i++) {
            for (int j = 0; j < alphabet.length(); j++) {
                if (sentenceArray[i] == alphabet.charAt(j)) {
                    for (int k = 0; k < 6; k++){
                        rowsToPrint[k] = rowsToPrint[k] + letters[j][k];
                    }
                }
            }
        }

        for (int k = 0; k < 6; k++){
            System.out.println(rowsToPrint[k]);
        }

Upvotes: 0

Andreas
Andreas

Reputation: 159135

Do not use Arrays.toString() for this. The letters are 6 lines tall, so your outermost loop has to iterate those 6 lines. Inside that you iterate the letters of the user input.

Like this:

  • Loop 6 lines: for (int line = 0; line < 6; line++)

    • Loop the letters of the user input: for (char letter : sentenceArray)

      • Print the appropriate "line" for the letter using print: TODO by You!
    • End the line: println()

Upvotes: 1

Related Questions