Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Program: (NumberFormatException) Write the bin2Dec(String binaryString) met

ID: 3675116 • Letter: J

Question

Java Program:

(NumberFormatException) Write the bin2Dec(String binaryString) method to convert a binary string into a decimal number. Implement the bin2Dec method to throw a NumberFormatException if the string is not a binary string.)

(BinaryFormatException) The exercise (above) implements the bin2Dec method to throw a BinaryFormatException if the string is not a binary string. Define a custom exception called BinaryFormatException. Implement the bin2Dec method to throw a BinaryFormatException if the string is not a binary string.

Explanation / Answer

/**The java program that prompts user to enter the string in binary string
* and converts the string to decimal . If the string is not in binary
* then throws NumberFormatException.*/
//BinaryToDecimal.java
import java.util.Scanner;
public class BinaryToDecimal
{
   public static void main(String[] args)
   {
       //Create an instnace of Scanner class
       Scanner scanner=new Scanner(System.in);
       String binaryString;
      
       System.out.println("Enter Binary string :");
       binaryString=scanner.nextLine();
      
       System.out.println("Binary String : "+binaryString);
       //Calling bin2Dec that converts the binary to decimal
       bin2Dec(binaryString);      
   }

   /**The method bin2Dec that takes string value and check if
   * string in binary format otherwise print the numbre format exception.
   * If the string is in binary format then converts the string to decimal
   * and prints to console*/
   private static void bin2Dec(String binaryString)
   {
       //set decimal to zero
       int decimal=0;
      
       try
       {
          
           for (int i = 0; i < binaryString.length(); i++)
           {
               //Parse from character to integer
               int n=Integer.parseInt(String.valueOf(binaryString.charAt(i)));
              
               //Throw exception if n is neither 0 or 1
               if(n!=0 && n!=1)
                   throw new NumberFormatException();
              
               //otherwise calculate the value of n in binary
               decimal+=n*Math.pow(2.0, i);
              
           }  
           //print the decimal value
           System.out.println("Decimal value : "+decimal);
       }
       catch (NumberFormatException e)
       {
           System.out.println("Exception: Number Format Exception");
       }      
   }//end of the method bin2dec
}

Sample output1:

Enter Binary string :
1111
Binary String : 1111
Decimal value : 15


Sample output2:
Enter Binary string :
123
Binary String : 123
Exception: Number Format Exception

-----------------------------------------------------------------------------------------------------------------------------------------------------------

//BinaryFormatException.java
//This class extends the Exception class that pass the argument
//message to the super class Exception
public class BinaryFormatException extends Exception
{
   public BinaryFormatException(String message)
   {
       super(message);
   }
}

----------------------------------------------------------------------------------------
/**The java program that prompts user to enter the string in binary string
* and converts the string to decimal . If the string is not in binary
* then throws BinaryFormatException.*/
//BinaryToDecimal.java
import java.util.Scanner;
public class CustomExceptionDriver
{
   public static void main(String[] args)
   {
       //Create an instnace of Scanner class
       Scanner scanner=new Scanner(System.in);
       String binaryString;
      
       System.out.println("Enter Binary string :");
       binaryString=scanner.nextLine();
      
       System.out.println("Binary String : "+binaryString);
       //Calling bin2Dec that converts the binary to decimal
       bin2Dec(binaryString);      
   }

   /**The method bin2Dec that takes string value and check if
   * string in binary format otherwise print the binary format exception.
   * If the string is in binary format then converts the string to decimal
   * and prints to console*/
   private static void bin2Dec(String binaryString)
   {
       //set decimal to zero
       int decimal=0;
      
       //try-catch block
       try
       {      
           for (int i = 0; i < binaryString.length(); i++)
           {
               //Parse from character to integer
               int n=Integer.parseInt(String.valueOf(binaryString.charAt(i)));
              
               //Throw exception if n is neither 0 or 1
               if(n!=0 && n!=1)
                   //Calling user defined BinaryFormatException
                   throw new BinaryFormatException("Binary Format Exception");
              
               //otherwise calculate the value of n in binary
               decimal+=n*Math.pow(2.0, i);
              
           }  
           //print the decimal value
           System.out.println("Decimal value : "+decimal);
       }
       catch (BinaryFormatException e)
       {
           System.out.println(e.getMessage());
       }      
   }//end of the method bin2dec
}

sample output2:
Enter Binary string :
1111
Binary String : 1111
Decimal value : 15


sample output2:

Enter Binary string :
123
Binary String : 123
Binary Format Exception

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote