简体   繁体   中英

Convert binary to decimal with an array

Im trying to make a binary string into a decimal. It will terminate if -1 is entered. I am stuck with using an array. It was suggested to use: public static int binaryToDecimal (String binaryString) . But Im not sure how to do that. This is what I have:

import java.util.Scanner;

public class BinaryConversion {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        String inString;
        int decimal;

        System.out.println("Enter a binary number: ");
        inString = input.nextLine();

        while (inString != "-1") {
            int i;
            int binaryLength;

            binaryLength = inString.length();

            for (i = 0, decimal = 0; i < binaryLength; i++) {
                decimal = decimal * 2 + (inString[i] - 0);
                System.out.print(decimal);

            } 
            System.out.println("Enter a binary number: ");
            inString = input.nextLine();
        }
        System.out.println("All set !");
    }
}

It says there is a compilation problem with the array. Thank you!

inString is a String, not an array. So, you can't use inString[i]. To get the character at a given position in the string, use inString.charAt(i), which returns a char.

Then, you'll also have to convert that char into an int. You can do this with Character.getNumericValue(char).

So in summary, instead of

inString[i]

you need to use

Character.getNumericValue(inString.charAt(i))

Try this one:

        public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        String inString;
        int decimal;

        System.out.println("Enter a binary number: ");
        inString = input.nextLine();
        //Character.getNumericValue(inString.charAt(i))

        while (inString != "-1") {
            int i;
            int binaryLength;

            binaryLength = inString.length();

            for (i = 0, decimal = 0; i < binaryLength; i++)
            {
                decimal = decimal * 2 + (Character.getNumericValue(inString.charAt(i)) - 0);
                System.out.print(decimal);

            } 
            System.out.println("Enter a binary number: ");
            inString = input.nextLine();
        }
        System.out.println("All set !");
    }

}

As suggested, you have to use Character.getNumericValue

You can simplify the code by using Integer.parseInt() :

public static void main(String[] args) {
    final Scanner input = new Scanner(System.in);

    String inString;

    while (true) {
        System.out.println("Enter a binary number: ");
        inString = input.nextLine();

        if (inString.equals("-1"))
            break;

        System.out.println(Integer.parseInt(inString, 2));
    }
    System.out.println("All set !");
}

You had few logical and few syntax errors.

This is working code :

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    String inString;
    int decimal;

    System.out.println("Enter a binary number: ");
    inString = input.nextLine();

    while (!"-1".equals(inString)) {
        int i;
        int binaryLength;

        binaryLength = inString.length();

        for (i = binaryLength-1, decimal = 0; i >= 0; i--) {
            if (inString.charAt(i) == '1') {
                decimal += Math.pow(2, binaryLength-i-1);
            }
        }
        System.out.println(decimal);

        System.out.println("Enter a binary number: ");
        inString = input.nextLine();
    }
    System.out.println("All set !");
}

Note that comparing String cannot be done with == , you have to use equals or compareTo methods.

byte[] binary = {1,1,0,1};

int decimal = 0;
for(int i=binary.length-1, j=0; i>=0; i--, j++){
    decimal  += binary[i]*Math.pow(2, j);
}

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