0

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!

5 Answers 5

1

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))
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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 !");
}

Comments

0

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.

Comments

0
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);
}

2 Comments

OP will be starting with a String, not a byte[].
Thank you for this code snippet, which may provide some immediate help. A proper explanation would greatly improve its educational value by showing why this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.