//package
import java.util.*;


// the main class BinaryToDecimalCalculator
public class BinaryToDecimalCalculator
{

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

		//declaration of input variables
       int binary,  exponent=0;
       float decimal, total=0  , currentNumber ;
       boolean error =false;//check for input errors

       //the user inputs a binary integer
       System.out.print("Enter binary number: ");
       binary = input.nextInt();


       //processing formular
       do
       {
		   
           //fetches binary digits
           decimal = (float)(binary)/((float)10);
           binary = binary / 10;
           currentNumber = (int)(10 * (decimal - binary));

           //prompts the user input when it is not a binary integer
           /*if(currentNumber >1 || )
           {
               System.out.println("The number you typed is not a binary value, re-run the program");
               error = true;
               break;
           }
           else
           {
			   //computes the decimal value
               total += (currentNumber * (Math.pow(2, exponent)));
               exponent++;
           }
       }while(decimal !=0);//checks whether extraction is finished

       if(!error)
       System.out.println("The decimal equivalent of binary integer is : "+(int)total);


    }
}

Edited 4 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

//package JOptionPane
import java.util.*;


// the main class BinaryToDecimalCalculator
public class BinaryToDecimalCalculator
{

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

		//declaration of input variables
       int binary,  exponent=0;
       float decimal, total=0  , currentNumber ;
       boolean error =false;//check for input errors

       //the user inputs a binary integer
       System.out.print("Enter binary number: ");
       binary = input.nextInt();


       //processing formular
       do
       {

           //fetches binary digits
           decimal = (float)(binary)%((float)10);
           binary = binary % 10;
           currentNumber = (int)(10 * (decimal - binary));

           //prompts the user input when it is not a binary integer
           if(currentNumber >1)
           {
               System.out.println("The number you typed is not a binary value, re-run the program");
               error = true;
               break;
           }
           else
           {
			   //computes the decimal value
               total += (currentNumber * (Math.pow(2, exponent)));
               exponent++;
           }
       }while(decimal !=0);

       if(!error)
       System.out.println("The decimal equivalent of binary integer is : "+(int)total);


    }
}

Edited 4 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post. Also more info than 'logic error' would be useful.

Please explain your problem. Show the programs output and add comments showing what the problem is.

For testing how to use operators, write a very small program that uses the operator and print out the results. Change the operands and do it again. Continue until you understand what is happening. If you have questions, post your code and questions here.

This article has been dead for over six months. Start a new discussion instead.