import java.util.Scanner; /** * Use a recursive function to calculate the value of binary number. */ public class Calculation { public static void main(String [] args) { Scanner input = new Scanner(System.in); System.out.println("Enter a binary number"); String binNum=input.nextLine(); int n= binNum.length(); int decNum = convert(binNum); if(decNum != -1) System.out.printf("The binary number, %s has value of %d.\n",binNum, decNum); } public static int convert(String x) { int n = x.length(); if( n == 0) return 0; else if(x.charAt(n-1) == '0') { x = x.substring(0, n-1); return 2 * convert(x); } else if(x.charAt(n-1) == '1') { x = x.substring(0, n-1); return 2 * convert(x) + 1; } else { System.out.println("You did not enter a binary number"); return -1; } } }