Decimal to Binary Converter using Java GUI

In this tutorial, i will teach you how to create a program that converts an inputted decimal number into binary using Java GUI. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of decToBinary.java. 2. Import javax.swing package. Hence we will use a GUI (Graphical User Interface) here like the inputting the decimal number.
  1. import javax.swing.*;
3. Initialize your variables in your Main, variable numInput as string and make it as an inputdialogbox. Variable n as integer that will parse the inputted value of numInput. And variable binary as integer that will hold the binary value of our input.
  1. int n;
  2. String numInput;
  3. String binary;
  4. numInput = JOptionPane.showInputDialog(null, "Enter a number:");
  5. n = Integer.parseInt(numInput);
4. Compute the equivalent binary value of the inputted decimal number.
  1. binary = Integer.toBinaryString(n);
toBinaryString method converts any number into a binary value. 5. Lastly, display the output of the program using JOptionPane.showMessageDialog.
  1. JOptionPane.showMessageDialog(null, "Binary equivalent is: " +binary);
Output: output Here's the full code of this tutorial:
  1. import javax.swing.*;
  2.  
  3. public class decToBinary {
  4.  
  5.  
  6. public static void main(String[] args) {
  7.  
  8.  
  9. int n;
  10. String numInput;
  11. String binary;
  12. numInput = JOptionPane.showInputDialog(null, "Enter a number:");
  13. n = Integer.parseInt(numInput);
  14.  
  15. binary = Integer.toBinaryString(n);
  16. JOptionPane.showMessageDialog(null, "Binary equivalent is: " +binary);
  17. }
  18.  
  19. }
Hope this program helps! :) Best Regards, Engr. Lyndon R. Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment