1

So the program is supposed to convert a binary number to a decimal number, by first checking if it is a binary number and then converting. No matter what input I give, it always says that the input is not a binary number and I can't figure out why. Here is my code.

public class Conversion {
    public static void main (String []args){
        String binaryNumber = args[0];
        if (checkBinary(binaryNumber)){
            int decimalNumber = binaryToDecimal(binaryNumber);
            System.out.print("The binary number " + binaryNumber + "is " + decimalNumber + "in base 10." );
        }
        else {
            System.out.println("The input is not binary!");
        }
    }

    public static boolean checkBinary(String binaryNumber){
        for (int i=0; i < binaryNumber.length(); i++) {
            char x = binaryNumber.charAt(i);
            if (x != '0' && x != '1'){
                return false;
            }
        }
        return true;
    }

    public static int binaryToDecimal(String binaryNumber) {
        int z = 1;
        int y = 0;
        for (int i = binaryNumber.length()- 1; i >= 0; i--){
            if (binaryNumber.charAt(i)=='1'){
                y += z;
            }
            z *= 2;
        }
        return y;
    }
}
5
  • Oddly the program as posted works for me. You might want to format your output a little. but I got the right answer: The binary number 101is 5in base 10. Commented Feb 20, 2016 at 18:40
  • Which compiler are you using? Commented Feb 20, 2016 at 18:41
  • Java 8 update 5; its the only one I have on this machine Commented Feb 20, 2016 at 18:43
  • Not directly to your question, but why not use Integer.parseInt(String, int) where the second argument is a radix (say 2). Commented Feb 20, 2016 at 18:45
  • You could add the input string to the error message "The input is not a binary," to verify that args[0] what you entered. Commented Feb 20, 2016 at 19:08

2 Answers 2

1

you just need to edit if (x != '0' && x != '1') to if (x != '0' || x != '1')

Sign up to request clarification or add additional context in comments.

Comments

1

Your class has this output, thats way thast run properly Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Conversion.main(Conversion.java:10) To avoid this kind the problems id much better use Scanne, so i just replace :

`String binaryNumber = args[0]; `

for

`    

 Scanner input = new Scanner(System.in);
 System.out.println("Enter Binary: ");
  String binaryNumber = input.next();`   

 

The whole code is bellow and run well(Java 8):

import java.util.Scanner;

public class Conversion {

    public static void main (String args[]){    
       // replace :String binaryNumber = args[0];
        Scanner input = new Scanner(System.in);
        System.out.println("Enter Binary: ");
        String binaryNumber = input.next();

        if (checkBinary(binaryNumber)){
            int decimalNumber = binaryToDecimal(binaryNumber);
            System.out.print("The binary number " + binaryNumber + " is " + decimalNumber + " in base 10." );
        }
        else {
            System.out.println("The input is not binary!");
        }
        input.close();
    }
    public static boolean checkBinary(String binaryNumber){
        for (int i=0; i = 0; i--){
            if (binaryNumber.charAt(i)=='1'){
                y += z;
            }
            z *= 2;
        }
        return y;
    }


}


 

OUTPUT : Enter Binary: 01000100111 The binary number 01000100111 is 551 in base 10.

Comments

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.