• We just launched and are currently in beta. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Decimal to Binary Converter using Java GUI

olsonton

Exploit Kit Developer
O Rep
0
0
0
Rep
0
O Vouches
0
0
0
Vouches
0
Posts
126
Likes
95
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
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:
bin.png


Here's the full code of this tutorial:

  1. import

    javax.swing.*

    ;

  2. public

    class

    decToBinary {


  3. public

    static

    void

    main(

    String

    [

    ]

    args)

    {


  4. int

    n;
  5. String

    numInput;
  6. String

    binary;
  7. numInput =

    JOptionPane

    .showInputDialog

    (

    null

    , "Enter a number:"

    )

    ;
  8. n =

    Integer

    .parseInt

    (

    numInput)

    ;

  9. binary =

    Integer

    .toBinaryString

    (

    n)

    ;
  10. JOptionPane

    .showMessageDialog

    (

    null

    , "Binary equivalent is: "

    +

    binary)

    ;
  11. }

  12. }

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


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

342,218

342,226

Top