-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBin2Dec.java
More file actions
90 lines (81 loc) · 2.09 KB
/
Bin2Dec.java
File metadata and controls
90 lines (81 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* Write a program called Bin2Dec to convert an input binary string
* into its equivalent decimal number. Your output shall look like:
*
* Enter a Binary string: 1011
* The equivalent decimal number for binary "1011" is 11
*
* Enter a Binary string: 1234
* Error: Invalid Binary String "1234"
*/
package javaexercises.keyboard;
import java.util.Scanner;
/**
*
* @author User
*/
public class Bin2Dec {
public static void main(String[] args) {
Bin2Dec aBin2Dec = new Bin2Dec();
aBin2Dec.runTest("1011");
aBin2Dec.runTest("0011");
aBin2Dec.runTest("1010");
aBin2Dec.runTest("1234");
// Scanner in = new Scanner(System.in);
// System.out.print("\nEnter a Binary string: ");
// String bin = in.next();
// aBin2Dec.runTest(bin);
}
/**
* Test.
*
* @param binStr
*/
private void runTest(String binStr)
{
if ( ! isBin(binStr)) {
System.out.printf("Error: Invalid Binary String \"%1$s\"\n", binStr);
return;
}
System.out.printf("The equivalent decimal number for binary \"%1$s\" is %2$d\n"
, binStr, convertBin2Dec(binStr));
}
/**
* Check if string represent a binary value.
*
* @param binStr
* @return
*/
private static boolean isBin(String binStr)
{
for(int i = 0; i < binStr.length(); i++)
{
if (binStr.charAt(i) == '0') {
continue;
}
if (binStr.charAt(i) == '1') {
continue;
}
return false;
}
return true;
}
/**
* Convert bin to decimal.
*
* @param bin
* @return int
*/
private int convertBin2Dec(String binStr)
{
int number = 0;
for(int i = 0; i < binStr.length(); i++)
{
if (binStr.charAt(i) == '0') {
continue;
}
number += Math.pow(2, (binStr.length() - 1 - i));
}
return number;
}
}