Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. //Ryan Hecht
  2. package net.ryanhecht.binaryclock;
  3.  
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class BinaryClock {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. /////////////////////Input///////////////////////////////
  12. System.out.println("Enter input (first line contains number of military times to display, followed by military times, one per line):");
  13. Scanner scanner = new Scanner (System.in);
  14.  
  15. //Gets number of times to be displayed
  16. int numoftimes = Integer.parseInt(scanner.nextLine());
  17.  
  18. //String array to store times
  19. String[] times=new String[numoftimes];
  20. //String array to store coded times
  21. String[][] bintimes=new String[numoftimes][6];
  22. //Input of times, stores in array
  23. for(int i=0;i<numoftimes;i++) {
  24. times[i] = scanner.nextLine();
  25. }
  26. ////////////////////////////////////////////////////////
  27.  
  28. //////////Coding into 4-bit Binary///////////////////////
  29.  
  30. //Cycles through each time
  31. for(int i=0;i<numoftimes;i++) {
  32. //Number of digits encoded into binary in current military time, used for storing coded values in bintimes array
  33. int digitsencoded=0;
  34.  
  35. //Cycles through each character in the time
  36. for(int j=0;j<8;j++) {
  37. //Checks to make sure the character is a digit and not ":"
  38. if(Character.isDigit(times[i].charAt(j))) {
  39. //Converts the character to a binary string (intermediary steps: character->string->int->binary string)
  40. String bs = Integer.toBinaryString(Integer.parseInt(Character.toString(times[i].charAt(j))));
  41. //Makes the binary string 4 bit if it isn't already
  42. for(int l=bs.length();l<4;l++) {
  43. bs = "0" + bs.substring(0);
  44. }
  45.  
  46. //Stores coded values in bintimes array, increments digitsencoded for next storage
  47. bintimes[i][digitsencoded] = bs;
  48. digitsencoded++;
  49. }
  50. }
  51. }
  52. /////////////////////////////////////////////////////////
  53.  
  54.  
  55. //////////////////////////OUTPUT/////////////////////////
  56.  
  57. //Cycles through each time
  58. for(int i=0;i<numoftimes;i++) {
  59. //Prints the military time as inputted (without ":")
  60. for(int j=0;j<8;j++) {
  61. if(Character.isDigit(times[i].charAt(j))) {
  62. System.out.print(times[i].charAt(j) + " ");
  63. }
  64. }
  65.  
  66. //Prints first digit of each
  67. System.out.println("");
  68. for(int j=0;j<6;j++) {
  69. System.out.print(bintimes[i][j].charAt(0) + " ");
  70. }
  71.  
  72. //Prints second digit of each
  73. System.out.println("");
  74. for(int j=0;j<6;j++) {
  75. System.out.print(bintimes[i][j].charAt(1) + " ");
  76. }
  77.  
  78. //Prints third digit of each
  79. System.out.println("");
  80. for(int j=0;j<6;j++) {
  81. System.out.print(bintimes[i][j].charAt(2) + " ");
  82. }
  83.  
  84. //Prints fourth digit of each
  85. System.out.println("");
  86. for(int j=0;j<6;j++) {
  87. System.out.print(bintimes[i][j].charAt(3) + " ");
  88. }
  89.  
  90. //New line for next input
  91. System.out.println("\n");
  92. }
  93.  
  94. ////////////////////////////////////////////////////////////
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement