Skip to content
Advertisement

Pattern generation in Java

I am trying to make a digital clock pattern using Java. However, I am having trouble printing the ‘digital’ values side-by-side, instead of one after the other. If I print each digit individually, they are rendered correctly, however, printing a specific time with 4 digits is not output correctly. I have tried to use looping to print the digital time, but could not get it to work. Therefore, I made each digit individually so I could print them side-by-side. Any help would be appreciated. Thank you.


Problem Statement:

Take time as input and output a multiline string representing the input, each digit made up of a 5 x 5 grid with a single space between each digit.

The separator is represented as a colon “:”, in its own 5 x 5 grid.

Example #1:

Input:

72:34

Output:

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

Example #2:

Input:

56:78

Output:

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

My Solution:

import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class DigitalClock {
    private static String colon;

    public static void main(String[] args) {
        // Example #1
        System.out.print(hours(7, 2) + colon + minutes(3, 4));
    }

    public static String hours(int left, int right) {
        return numbers(left) + numbers(right);
    }

    public static String minutes(int left, int right) {
        return numbers(left) + numbers(right);
    }

    private static String numbers(int number) {
        String space = repeat(" ", 4);
        String newLine = "n";
        String colonMaker = space + "#" + space;
        colon = newLine + colonMaker +
                repeat(newLine, 2) + colonMaker + newLine;
        String right =
                space + repeat("#n" + space, 4) + "#";

        String top = "#####";
        String topLeft = top + newLine + "#";
        String topRight = top + newLine + space + "#";

        String middle = repeat(newLine, 1) + top;
        String middleLeft = newLine + "#" + newLine + top;
        String middleRight = newLine + space + "#" + newLine + top;

        String bottom = repeat(newLine, 1) + top;
        String bottomLeft = newLine + "#" + newLine + top;
        String extra = "#   #";
        String bottomRight = newLine + space + "#n" + top;

        String[] digitalFormat = new String[10];
        digitalFormat[0] = top + repeat(extra + "n", 3);
        digitalFormat[1] = right;
        digitalFormat[2] = top + middleRight + bottomLeft;
        digitalFormat[3] = topRight + middle + bottomRight;
        digitalFormat[4] = extra + newLine + extra + middle + newLine + space + "#n" + space + "#";
        digitalFormat[5] = top + middleLeft + bottomRight;
        digitalFormat[6] = top + middleLeft + newLine + extra + bottom;
        digitalFormat[7] = top + repeat(newLine + space + "#", 4);
        digitalFormat[8] = top + newLine + extra + middle +
                newLine + extra + bottom;
        digitalFormat[9] = top + newLine + extra + middle + newLine + space + "#n" + space + "#";
        return digitalFormat[number];
    }

    private static String repeat(String input, int times) {
        return IntStream.range(0, times).mapToObj(i -> input).collect(Collectors.joining(""));
    }
}

Output:

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

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

Advertisement

Answer

Try this.

static final Map<Character, String[]> CHARS = new HashMap<>();
static {
    CHARS.put(':', new String[] {"     ", "  #  ", "     ", "  #  ", "     "});
    CHARS.put('0', new String[] {"#####", "#   #", "#   #", "#   #", "#####"});
    CHARS.put('1', new String[] {"    #", "    #", "    #", "    #", "    #"});
    CHARS.put('2', new String[] {"#####", "    #", "#####", "#    ", "#####"});
    CHARS.put('3', new String[] {"#####", "    #", "#####", "    #", "#####"});
        /* ....... */
    CHARS.put('9', new String[] {"#####", "#   #", "#####", "    #", "#####"});
}

static String[] pattern(String s) {
    int height = CHARS.values().iterator().next().length;
    int length = s.length();
    String[][] strs = s.chars()
        .mapToObj(c -> CHARS.get((char)c))
        .toArray(String[][]::new);
    return IntStream.range(0, height)
        .mapToObj(i -> IntStream.range(0, length)
            .mapToObj(j -> strs[j][i])
            .collect(Collectors.joining(" ")))
        .toArray(String[]::new);
}

public static void main(String[] args) {
    Stream.of(pattern("02:39")).forEach(System.out::println);
}

output:

##### #####       ##### #####
#   #     #   #       # #   #
#   # #####       ##### #####
#   # #       #       #     #
##### #####       ##### #####
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement