newguy
newguy

Reputation: 53

Why does my program keep returning not a binary number?

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

Upvotes: 1

Views: 71

Answers (2)

Stefano Castagnino
Stefano Castagnino

Reputation: 47

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.

Upvotes: 1

ernestk
ernestk

Reputation: 72

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

Upvotes: 1

Related Questions