Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Ryan Hecht
- package net.ryanhecht.binaryclock;
- import java.util.Scanner;
- public class BinaryClock {
- public static void main(String[] args) {
- /////////////////////Input///////////////////////////////
- System.out.println("Enter input (first line contains number of military times to display, followed by military times, one per line):");
- Scanner scanner = new Scanner (System.in);
- //Gets number of times to be displayed
- int numoftimes = Integer.parseInt(scanner.nextLine());
- //String array to store times
- String[] times=new String[numoftimes];
- //String array to store coded times
- String[][] bintimes=new String[numoftimes][6];
- //Input of times, stores in array
- for(int i=0;i<numoftimes;i++) {
- times[i] = scanner.nextLine();
- }
- ////////////////////////////////////////////////////////
- //////////Coding into 4-bit Binary///////////////////////
- //Cycles through each time
- for(int i=0;i<numoftimes;i++) {
- //Number of digits encoded into binary in current military time, used for storing coded values in bintimes array
- int digitsencoded=0;
- //Cycles through each character in the time
- for(int j=0;j<8;j++) {
- //Checks to make sure the character is a digit and not ":"
- if(Character.isDigit(times[i].charAt(j))) {
- //Converts the character to a binary string (intermediary steps: character->string->int->binary string)
- String bs = Integer.toBinaryString(Integer.parseInt(Character.toString(times[i].charAt(j))));
- //Makes the binary string 4 bit if it isn't already
- for(int l=bs.length();l<4;l++) {
- bs = "0" + bs.substring(0);
- }
- //Stores coded values in bintimes array, increments digitsencoded for next storage
- bintimes[i][digitsencoded] = bs;
- digitsencoded++;
- }
- }
- }
- /////////////////////////////////////////////////////////
- //////////////////////////OUTPUT/////////////////////////
- //Cycles through each time
- for(int i=0;i<numoftimes;i++) {
- //Prints the military time as inputted (without ":")
- for(int j=0;j<8;j++) {
- if(Character.isDigit(times[i].charAt(j))) {
- System.out.print(times[i].charAt(j) + " ");
- }
- }
- //Prints first digit of each
- System.out.println("");
- for(int j=0;j<6;j++) {
- System.out.print(bintimes[i][j].charAt(0) + " ");
- }
- //Prints second digit of each
- System.out.println("");
- for(int j=0;j<6;j++) {
- System.out.print(bintimes[i][j].charAt(1) + " ");
- }
- //Prints third digit of each
- System.out.println("");
- for(int j=0;j<6;j++) {
- System.out.print(bintimes[i][j].charAt(2) + " ");
- }
- //Prints fourth digit of each
- System.out.println("");
- for(int j=0;j<6;j++) {
- System.out.print(bintimes[i][j].charAt(3) + " ");
- }
- //New line for next input
- System.out.println("\n");
- }
- ////////////////////////////////////////////////////////////
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement