简体   繁体   中英

How to make my binary to decimal conversion program also read 0s?

This program is supposed to convert binary numbers to decimal and throws the exception when the input has non-binary numbers. This program will read 1s, but when I input 0s, it will throw the exception and tell me it's not binary.

Test Program:

//Prepare scanner from utility for input.
import java.util.Scanner;

public class Bin2Dec {
    public static void main (String[] args){
        //Convert the input string to their decimal equivalent.
        //Open scanner for input.
        Scanner input = new Scanner(System.in);
        //Declare variable s.
        String s;

        //Prompt user to enter binary string of 0s and 1s.
        System.out.print("Enter a binary string of 0s and 1s: ");
        //Save input to s variable.
        s = input.nextLine();
        //With the input, use try-catch blocks.
        //Print statement if input is valid with the conversion.
        try {
            System.out.println("The decimal value of the binary number "+ "'" + s + "'" +" is "+conversion(s));
            //Catch the exception if input is invalid.
        } catch (BinaryFormatException e) {
            //If invalid, print the error message from BinaryFormatException.
            System.out.println(e.getMessage());
        }
    }
    //Declare exception.
    public static int conversion(String parameter) throws BinaryFormatException {
        int digit = 0;
        for (int i = parameter.length(); i > 0; i--) {
            char wrong_number = parameter.charAt(i - 1);
            if (wrong_number == '1') digit += Math.pow(2, parameter.length() - i);
            //Make an else statement and throw an exception.

            else if (wrong_number == '0') digit += Math.pow(2, parameter.length() - i);

            else 
                throw new BinaryFormatException("");
        }
        return digit;
    } 
}

This program only accepts '1' as char due to these lines:

if (wrong_number == '1') digit += Math.pow(2, parameter.length() - i);
          //Make an else statement and throw an exception.
else 
    throw new BinaryFormatException("");

Since there's no if(wrong_number == '0') , the number will only accept 1s and throw an exception when encountering a 0.

Apart from that: Avoid Math.pow , if possible in any way. Since it's quite resource intensive and in this case completely useless. 2^x can be generated a lot easier using bit-shifting:

int pow_2_x = (1 << x);

And finally: java already provides a method for this:

int dec = Integer.parseInt(input_string , 2);

Problem is with your logic. Since you are dealing with binary numbers ('1' and '0') but you are checking for 1 only you should check for '0' also and throw exception only if it's other than '0' ans '1'

if (wrong_number == '1') digit += Math.pow(2, parameter.length() - i);
//Make an else statement and throw an exception.


else 
    throw new BinaryFormatException("");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM