Reputation: 3333
I am just wondering whether is it possible to display number in digital format. In this we are accepting number from console.
For example:
123 should be display in following format, and in one line.
_ _
| _| _|
| |_ _|
I tried using switch case but not got appropriate method
"arg" is char array;
char c;
for(i=0;i<3;i++)
{
c=arg[i];
switch(char)
{
case '1' : System.out.println("|");
System.out.println("|");
break;
case '2' : System.out.println("-");
System.out.println(" "+"|");
System.out.println("-");
System.out.println("|");
System.out.println("-");
break;
same for all digit
}
}
I know this is not a correct solution to display.
Is it any alternative for this using java.
EDIT Updated code which is suggested by Joe. It works.
/**
* @author Amit
*/
public class DigitalDisplay {
/**
* @param args
*/
public static void main(String[] args) {
String [][] num=new String[4][3];
num[0][0]="|-|";
num[0][1]="| |";
num[0][2]="|_|";
num[1][0]=" |";
num[1][1]=" |";
num[1][2]=" |";
num[2][0]=" -|";
num[2][1]=" _|";
num[2][2]=" |_";
num[3][0]=" -|";
num[3][1]=" _|";
num[3][2]=" _|";
int[] input = {2,1,3};
for (int line = 0; line < 3; line++)
{
for (int inputI=0; inputI < input.length; inputI++)
{
int value = input[inputI];
System.out.print(num[value][line]);
System.out.print(" ");
}
System.out.println();
}
}
}
**OUTPUT**
-| | -|
_| | _|
|_ | _|
@Joe: Thanks.
Upvotes: 2
Views: 11935
Reputation: 1
//this program help you
import java.util.*;
public class Test{
public static void main(String[] args) throws Exception {
Scanner inr = new Scanner(System.in);
char c = inr.next().charAt(0);
// Add the necessary code in the below space
if(c=='2'||c=='3'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9'||c=='0')
System.out.println(" _ ");
else
System.out.println(" ");
if(c=='4'||c=='5'||c=='6'||c=='8'||c=='9'||c=='0')
System.out.print("|");
else
System.out.print(" ");
if(c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='8'||c=='9')
System.out.print("_");
else
System.out.print(" ");
if(c=='1'||c=='2'||c=='3'||c=='4'||c=='7'||c=='0'||c=='8'||c=='9')
System.out.println("|");
else
System.out.println(" ");
if(c=='6'||c=='2'||c=='8'||c=='0')
System.out.print("|");
else
System.out.print(" ");
if(c=='2'||c=='3'||c=='5'||c=='6'||c=='8'||c=='9'||c=='0')
System.out.print("_");
else
System.out.print(" ");
if(c=='1'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9'||c=='0')
System.out.println("|");
else
System.out.println(" ");
}
}
Upvotes: 0
Reputation: 47609
If you have more than one character, you need to scan across each line, building it up.
All of the following is pseudocode, but I am sure you can translate it into Java. You will need to create a grid and ensure that each digit has the same width and height.
1 - Set up your initial data for each number. You could do this as a 2d array (dimension 1: digit, dimension 2: line) For example:
numbers[0][0] = "|-|"
numbers[0][1] = "| |"
numbers[0][2] = "|_|"
numbers[1][0] = " |"
numbers[1][1] = " |"
numbers[1][2] = " |"
2 - For each line that makes up the character, scan across each digit in your input and write the appropriate string. This will build up the characters line by line.
input = [1,2,3]
digit_height = 3
for line digit_height:
for digit in input:
print numbers[digit][line]
print " " to space between characters
print new line
Fine, here's the java:
int[] input = {2,1,3};
for (int line = 0; line < 3; line++)
{
for (int inputI=0; inputI < input.length; inputI++)
{
int value = input[inputI];
System.out.print(num[value][line]);
System.out.print(" ");
}
System.out.println();
}
Upvotes: 4
Reputation: 15052
The simplest method would be, draw an 8, and store it. Then on the basis of the required number, you would know which stroke of 8 has to be printed, and which doesn't need to be printed.
Example:
8:
_
|_|
|_|
For 1, just print the right half,
|
|
For 2, ignore the corresponding strokes,
_
_|
|_
Similarly for others. This is how a 7 segment display actually works as well.
Have an array store the values of the 7 strokes needed.
Then for 8, your array could be something like : {1,1,1,1,1,1,1}
For 1, your array could be like : {0,0,0,1,0,0,1}
where 0 means not to print the stroke, and 1 means to print the stroke.
Then on the basis of this array, print the strokes. It is better than simply having a switch
case and printing the corresponding for every number. Just loop through the array elements, deciding whether or not to print the stroke.
For the example you have given,123,the have a 2D array. The values can go {{0,0,0,1,0,0,1},{1,0,1,1,1,1,0},{1,0,1,1,0,1,1}}
Then compare the values from the array and print your strokes!
EDIT: sample code:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
s = br.readLine();
int n;
n = Integer.parseInt(s);
int stroke = 1;
int arr[] = new int[8];
if(s == 2)
arr[] = {1,0,1,1,1,1,0};
for(int i=0;i<8;i++)
{
if(arr[i] == 1 && stroke = 1)
System.out.println(" _");
stroke++;
break;
if(arr[i] == 1 && stroke = 2)
System.out.print("|");
stroke++;
break;
if(arr[i] == 1 && stroke = 3)
System.out.print("_");
stroke++;
break;
if(arr[i] == 1 && stroke = 4)
System.out.println("|");
stroke++;
break;
if(arr[i] == 1 && stroke = 5)
System.out.print("|");
stroke++;
break;
if(arr[i] == 1 && stroke = 6)
System.out.println("_");
stroke++;
break;
if(arr[i] == 1 && stroke = 7)
System.out.println("|");
stroke++;
break;
}
Once one number is printed, you can use the JLine library to replace your cursor on your console to start over with the next number.(Though I'm not sure about it).
It might require a little bit more tweaking to be exactly the answer required!
Upvotes: 1
Reputation: 3
You can make a new class with the output of the numbers plus a function to store what you want to print and a function to print.
Example:
public class DigitalNumbers{
private String buffer = "";
private int lines = 5; // Number's height
private String number0[] = {
"___ ",
"| | ",
"| | ",
"| | ",
"¯¯¯ "
};
private String number1[] = {
" ",
"| ",
"| ",
"| ",
" "
};
//...
public DigitalNumbers(){
}
public void add(char c){
buffer += c;
}
public void print(){
for(int i=0; i<lines; i++){
for(int j=0; j<buffer.length(); j++){
switch(buffer.charAt(j)){
case '0': System.out.print(number0[i]); break;
case '1': System.out.print(number1[i]); break;
//...
default: break;
}
}
System.out.print("\n");
}
buffer = "";
}
}
And then use like this:
DigitalNumbers DG = new DigitalNumbers();
for(int i=0;i<3;i++){
DN.add(arg[i]);
}
DG.print();
Upvotes: 0
Reputation: 9478
Here is some example on digital clock, it may helpfull to you Digital clock
Upvotes: 1
Reputation: 359826
There are at least 3 different, very basic syntax errors here, not to mention any logical errors.
char
is a reserved word, so you cannot use it for a variable name.switch
is written lowercased (like all other keywords in Java)"1"
is a string literal, not a char
literal. Use single quotes for char
literals.Upvotes: 0