Home Java Tutorials Decimal to Binary Java Source Code Using toBinaryString Method

Software Configurations

Follow Me

Decimal to Binary Java Source Code Using toBinaryString Method

This java source code will shows how to convert decimal number to binary number using toBinaryString(integer)  method.

Decimaltobinary.java

 

//copyrighted by topsourcecode.com
import javax.swing.JOptionPane;

public class Decimaltobinary 
{
 public static void main(String []a)
 {
    //Method 1
    String sNumber=JOptionPane.showInputDialog("Insert a Integer value");
    int number=0;
    try
    {
      number=Integer.parseInt(sNumber);
    }
    catch(Exception e)
    {
       JOptionPane one = new JOptionPane();
       JOptionPane.showMessageDialog(one,"Input value error(Expected a decimal value)");
       System.exit(-1);
    }
    
    String binary = Integer.toBinaryString(number);
    JOptionPane one = new JOptionPane();
    JOptionPane.showMessageDialog(one,"Binary Value of Decimal "+number+" =\n" + binary);
 }
}

 

Result

Share this post