public class exercise13_7 { public static void main(String[] args) { //user to enter a string java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter a binary number string: "); String s = input.nextLine(); //if string is a binary string then print this System.out.println("The decimal value is " + binaryToDecimal(s)); //else string is not a binary string then //throw new NumberFormatException("Given string is not in binary format."); } public static int binaryToDecimal(String binaryString) { int value = binaryString.charAt(0)  '0'; for (int i = 1; i < binaryString.length(); i++) { value = value * 2 + binaryString.charAt(i)  '0'; } return value; } }
Binary to decimal number format exception
Page 1 of 13 Replies  3731 Views  Last Post: 16 February 2012  06:16 PM
#1
Binary to decimal number format exception
Posted 16 February 2012  05:19 PM
I need to write a program that converts binary to decimal. I have figured that part of the code out. The problem that i m having is i need it to throw a number format exception if the string is not a binary string. All i can think of is to use a if else statement to get it to print the decimal value or not in binary format. But i don't know how to make it know that the user entered 1's and 0's. Any help will be appreciated thanks
Replies To: Binary to decimal number format exception
#2
Re: Binary to decimal number format exception
Posted 16 February 2012  05:45 PM
You can check if the string have only 0 and 1 using java regex, for example:
Pattern p = Pattern.compile("^[01]+$"); Matcher m = p.matcher("1110ee"); if(m.find()) { System.out.println("binary"); }else{ System.out.println("not binary"); throw new NumberFormatException("Given string is not in binary format."); }
This post has been edited by guidogranobles: 16 February 2012  05:46 PM
#3
Re: Binary to decimal number format exception
Posted 16 February 2012  05:53 PM
For Binary, the number is 2 to the index power. So the very far right, where it will be 1 or 0, is 2^0 which is 1, if there is a 1 there then you add 1, then the next number would be 2^1 power which is 2, so if you have a 1 there add 2, then the next one is 2^2 power which is 4, so if you have a 1 there then add 4.
So, now that you know that, create a loop that starts at the string length 1, and goes until >=0, then decreases by 1. Have a binaryPosTracker to keep track of the index you should be at. Check and see if the char at the index is a 0 or a 1, if it is a 1 do the math, else move on.
Ex. 
Here is the method you will need  Math.pow(double base, double exponent)
Make sense?
So, now that you know that, create a loop that starts at the string length 1, and goes until >=0, then decreases by 1. Have a binaryPosTracker to keep track of the index you should be at. Check and see if the char at the index is a 0 or a 1, if it is a 1 do the math, else move on.
Ex. 
String binary = "1010"; int decimal = 0; int binaryPosTracker = 0; for(int i = /*Set the conditionals as I stated above*/) { if(/* the char at binary's i index == '1' */) decimal+= (int)(/*Cast to an int since method returns a double, also put this in parenthesis  Use Math.pow((double)base,(double)exponent) to get value */) /* increase binaryPosTracker no matter what so that the index stays correct*/ } System.out.println("The binary value of: " + binary + " = " + decimal);
Here is the method you will need  Math.pow(double base, double exponent)
Make sense?
#4
Re: Binary to decimal number format exception
Posted 16 February 2012  06:16 PM
Yes what you said makes sense but I have already did it with the java regex. thanks
Page 1 of 1
