//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);


    }
}

Recommended Answers

All 3 Replies

//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);


    }
}

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.

Threads merged.

@popsyjonhson: Please do not create multiple threads for a single question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.