user3462077
user3462077

Reputation: 17

java binary to decimal

Im having trouble converting binary to a decimal. We have to use a function for the conversion and do it by hand rather than use a predefined function. This is what I have so far, I know it is a mess but I am stuck on how to fix it. Thanks!

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 (!"-1".equals(inString)) {
        int i;
        int binaryLength;

        binaryLength = inString.length();
public static int binaryToDecimal (String binaryString) {

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

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

Upvotes: 1

Views: 1027

Answers (4)

Ingo
Ingo

Reputation: 36339

The confusing part is with the Math.pow, and its complicated arguments, where off-by-one errors are easily made.

Yet, if we have a number at base 10, like

123

its value is

(((0*10)+1)*10+2)*10+3

This looks complex, but note the easy pattern: Starting out with 0, we go through the digits. As long as we have another dgit, we multiply the previous result by the base and add the digit value. That's all! No Math.pow, no complex index calculations.

Hence:

String s = "1010";
int value = 0;
int base = 2;
for (i=0; i < s.length(); s++) {
    char c = s.charAt(i);
    value = value * base;
    value = value + c - '0';
}

Upvotes: 1

amunozdv
amunozdv

Reputation: 111

Here's the function after a little clean up.

        public static int binaryToDecimal (String binaryString) {
            int decimal = 0;
            int base    = 2;
            for (int i = binaryString.length() - 1; i >= 0; i--) {
                if (binaryString.charAt(i) == '1')
                    decimal += Math.pow(base,i);
            }
            return decimal;
        }

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

When I cleaned up your code, it worked just fine -

public static int binaryToDecimal(String binaryString) {
    int binaryLength = binaryString.length();
    int decimal = 0;
    for (int i = binaryLength - 1; i >= 0; i--) {
        if (binaryString.charAt(i) == '1') {
            decimal += Math.pow(2, binaryLength - 1 - i);
        }
    }
    return decimal;
}

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a binary number: ");
    String inString = input.nextLine();

    while (!"-1".equals(inString)) {
        System.out.println(binaryToDecimal(inString));

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

Output

Enter a binary number: 
01
1
Enter a binary number: 
10
2
Enter a binary number: 
-1
All set !

Upvotes: 0

ajb
ajb

Reputation: 31699

To use a function, as your assignment requires, you have to write the function outside the main method, and then include a statement that calls the function. So move this above the line that says public static void main:

public static int binaryToDecimal (String binaryString) {

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

Also, each function or method (including main) has its own variables that it uses, called local variables; but the local variables that each function uses are its own separate copies. Thus, the above function won't be able to use the binaryLength or decimal variabes belonging to main. You'll need to declare them inside binaryToDecimal:

public static int binaryToDecimal (String binaryString) {

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

Also, this function won't be able to access main's inString, but the idea is that you've given the function the string you want to work with, which it refers to as binaryString. So change inString to binaryString in the function:

public static int binaryToDecimal (String binaryString) {

    int decimal;
    int binaryLength;
    for (i = binaryLength - 1, decimal = 0; i >= 0; i--) {
        if (binaryString.charAt(i) == '1')
            decimal = decimal + Math.pow(2,binaryString.length() - 1 - i);
    }
    return (int) decimal;
}

And also note that the binaryLength and decimal variables are totally unrelated to the variables of the same name in main. That means that when you assigned binaryLength in main, that has no effect on binaryLength in binaryToDecimal. You'll need to assign it in the function. Change int binaryLength; to

int binaryLength = binaryString.length();

Finally, in order to use the function, main will need to call it. Put this in the main function:

decimal = binaryToDecimal(inString);

When main executes that, it will call the function and tell it to work with inString. The function will call that binaryString, though. The function will return a result, and then main will assign that result to the variable decimal--that means the local variable decimal that belongs to main, since the above statement is inside main.

I don't know if this will make your whole program work. (It should, but I'm not sure.) But I'm just trying to explain the details of how to use functions.

Upvotes: 2

Related Questions