|
| 1 | + |
| 2 | +import java.util.Scanner; |
| 3 | + |
| 4 | + |
| 5 | +public class NumberConverter { |
| 6 | + |
| 7 | + private String binaryString; |
| 8 | + private Scanner kbd; |
| 9 | +public NumberConverter() { |
| 10 | + kbd = new Scanner(System.in); |
| 11 | + |
| 12 | +//obtain binary string |
| 13 | +System.out.println("Enter a binary number (only 1's and 0's)"); |
| 14 | +binaryString = kbd.next().trim(); |
| 15 | +boolean booleanIsValid = booleanValidate(binaryString); |
| 16 | + |
| 17 | +if(booleanIsValid) { |
| 18 | +//if the number input is a valid boolean stream, get decimal and hexadecimal correspondents and display |
| 19 | +System.out.println(String.format("Decimal Value: %d", convertToDecimal(binaryString))); |
| 20 | +System.out.println(String.format("Hexadecimal Value: %s", convertToHex(binaryString))); |
| 21 | +} |
| 22 | +else { |
| 23 | +System.out.println("You input an invalid binary stream.\n(binary numbers do not contain digits greater than 1\nand contain no minus signs or other mathematical symbols"); |
| 24 | +} |
| 25 | + |
| 26 | +} |
| 27 | + |
| 28 | +private boolean booleanValidate(String binaryString) { |
| 29 | +for(int i= binaryString.length(); i>0; i--) { |
| 30 | + |
| 31 | + String character = binaryString.substring(i-1, i); |
| 32 | + if(!character.matches("\\d")) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + if(Integer.parseInt(character) > 1 ) { |
| 36 | + return false; |
| 37 | + } |
| 38 | +} |
| 39 | +return true; |
| 40 | +} |
| 41 | + |
| 42 | +private int convertToDecimal(String binary) { |
| 43 | +int tmpvalue; |
| 44 | +int result =0; |
| 45 | +int numberOfDigits = binary.length(); |
| 46 | +for(int i= numberOfDigits; i>0; i--) { |
| 47 | +tmpvalue = Integer.parseInt(binary.substring(i-1,i)); |
| 48 | +result += tmpvalue*(int)Math.pow(2, numberOfDigits-i); |
| 49 | +} |
| 50 | +return result; |
| 51 | +} |
| 52 | + |
| 53 | +private String convertToHex(String binary) { |
| 54 | +String result =""; |
| 55 | +String tmpvalue=""; |
| 56 | +int tempCount=0; |
| 57 | +int tempresult; |
| 58 | + |
| 59 | +int numberOfDigits = binary.length(); |
| 60 | +for(int i= numberOfDigits; i>0; i--) { |
| 61 | +tmpvalue = binary.substring(i-1, i).concat(tmpvalue); |
| 62 | +tempCount++; |
| 63 | +if (tempCount == 4 || i==1 ) { |
| 64 | +tempresult = convertToDecimal(tmpvalue); |
| 65 | +switch(tempresult) { |
| 66 | +case 10: |
| 67 | +result = "A".concat(result); |
| 68 | +break; |
| 69 | +case 11: |
| 70 | +result = "B".concat(result); |
| 71 | +break; |
| 72 | +case 12: |
| 73 | +result = "C".concat(result); |
| 74 | +break; |
| 75 | +case 13: |
| 76 | +result = "D".concat(result); |
| 77 | +break; |
| 78 | +case 14: |
| 79 | +result = "E".concat(result); |
| 80 | +break; |
| 81 | +case 15: |
| 82 | +result = "F".concat(result); |
| 83 | +break; |
| 84 | +default: |
| 85 | +result = Integer.toString(tempresult).concat(result); |
| 86 | +} |
| 87 | +tmpvalue = ""; |
| 88 | +tempCount = 0; |
| 89 | +} |
| 90 | +} |
| 91 | +return result; |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +public static void main(String[] args) { |
| 96 | + |
| 97 | + NumberConverter nc = new NumberConverter(); |
| 98 | + |
| 99 | +} |
| 100 | +} |
0 commit comments