Reputation: 87
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:
#####
#
#
#
######
#
#####
#
#####
#
#
#####
#
#####
#
###### #
# #
#####
#
#
Upvotes: 0
Views: 588
Reputation:
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:
##### ##### ##### #####
# # # # # # #
# # ##### ##### #####
# # # # # #
##### ##### ##### #####
Upvotes: 1
Reputation: 481
Here is a simple solution:
public class DigitalClock
{
public static void main (String[] args)
{
Digit result =
new Digit ('0').join (new Digit (':')).join (new Digit ('0')).join (new Digit (':')).join (new Digit ('1'));
for (int i = 0; i < result.pixels.length; i++)
{
System.out.println (result.pixels[i]);
}
System.out.println ();
System.out.println ();
System.out.println ();
Digit result2 = Digit.forString ("0:1 1:0");
for (int i = 0; i < result2.pixels.length; i++)
{
System.out.println (result2.pixels[i]);
}
}
}
class Digit
{
String[] pixels;
Digit (char ch)
{
switch (ch)
{
case ' ':
pixels = new String[] {" ".repeat (5), " ".repeat (5), " ".repeat (5), " ".repeat (5), " ".repeat (5)};
break;
case ':':
pixels = new String[] {" ".repeat (5), " * ", " ".repeat (5), " * ", " ".repeat (5)};
break;
case '0':
pixels = new String[] {"*****", "* *", "* *", "* *", "*****"};
break;
case '1':
pixels = new String[] {" * ", " * ", " * ", " * ", " * "};
break;
// Fill in cases for '2' through 9'
default:
throw new Error ("Unknown digit");
}
}
private Digit (String[] pixels)
{
this.pixels = pixels;
}
public Digit join (Digit d)
{
String[] result = new String[5];
for (int i = 0; i < pixels.length; i++)
{
result[i] = pixels[i] + " " + d.pixels[i];
}
return new Digit (result);
}
/** Convenience method for doing lots of digit joins. */
public static Digit forString (String s)
{
Digit base = new Digit (s.charAt (0));
Digit result = base;
for (int i = 1; i < s.length (); i++)
{
result = result.join (new Digit (s.charAt (i)));
}
return result;
}
}
Upvotes: 1
Reputation: 2276
Since this question seems to be about some sort of assignment, I will avoid posting a specific solution. Please refer to How do I ask and answer homework questions? to read up on what information you should provide in such a case.
As you already tested, you correctly print single digits, for example
#####
#
#
#
#
which, written in one line, is
#####\n #\n #\n #\n #
and
+++++
+
+++++
+
+++++
which, written in one line, is
+++++\n +\n+++++\n+ \n+++++
I used +
to render the second digit to make a more visible distinction between the individual digits.
Now, if you concatenate these two as by
numbers(left) + numbers(right)
you will get
#####\n #\n #\n #\n #+++++\n +\n+++++\n+ \n+++++
Which, written in multiple lines, is
#####
#
#
#
#+++++
+
+++++
+
+++++
As you can see, we first have all the lines of the first digit and then all the lines of the second digit with the last line of the first digit and the first line of the second digit joined together.
So, to correctly concatenate these rendered digits, you'll have to join the digit's lines individually, hence instead of
#####\n #\n #\n #\n #+++++\n +\n+++++\n+ \n+++++
you have to somehow get to
#####+++++\n # +\n #+++++\n #+ \n #+++++
which, in multiple lines, is
#####+++++
# +
#+++++
#+
#+++++
Upvotes: 3