Learn To Create A Basic Calculator In Java Using Netbeans

Basic Calculator Program In Java

In this article, we will be learning how to create a Basic Calculator Program in Java using the NetBeans. We will start by installing NetBeans.

Installation of NetBeans
The Java SE Development Kit(JDK) 7 or JDK 8 is required to install the NetBeans IDE. Now download the latest NetBeans IDE from this link.

Download the whole IDE bundle version depending upon your processor architecture. The user can add or remove the packs using the IDE’s Plugin Manager.


Assume that you have successfully installed NetBeans on your machine. Start NetBeans from the Window. The NetBeans main window will appear as shown below.


The NetBeans main window contains menus, toolbars, project pane, files pane, runtime pane, navigator pane, and other similar panes. The NetBeans main window is the command center for the IDE.

Creating a Blank Project
To create and run a program, you have to first create a project. Here are the steps to create a demo project.

Step 1: – Choose File, New Project to display the New Project Dialog box.


The new project box enables you to specify the project type.
Step 2: – Choose the Java type from the given list.
Step 3: – From the New Application Tab, enter the project name and unselect the main class from it.

Java Calculator
Step 1: – Create a blank Java project and name it Java Calculator. Uncheck the Main class option from the project tab.
Step 2: – To add a Jframe, right click the project name in the Project window.


Step 3: – To the right of the Jframe, there is a Palette with a set of controls on it. Click the Text Field and apply it on the blank Field form. Now click Properties, then change the text field to blank. Click on the Code and change the Variable Name to txtDisplay.



Step 4: – Select the Button and drag it to the Jframe form. Change the text and the Variable Name as we did for the text field.


Step5: – Change the Variable Name to btnOne as we did for the text Field. Enter the value 1 for the text by selecting the properties. Do the same for all the buttons by changing the text and the Variable Name.
The Final Jframe structure will be formed.


Step6: – Double click on any of the button. This will take the user to the source section where the major coding of the program is done. The image below depicts the Source section.


Step7: – Declare the following variables inside the main class.


The firstnum term is to accept the first number entered, whereas the secondnum is to accept the second number stored. The variable result is used to store the output value.

Writing Code for the Number Button on Java Calculator
To get a text from a form object, you can use the getText method of the object (if it has one). So to get the text from btnOne we could use this code:

String Enternumber = txtDisplay.getText()+btnOne.getText();

Similarly, for the btnTwo, we could use the similar code:

String Enternumber = txtDisplay.getText()+btnTwo.getText()


Ultimate Java Development and Certification Course

Arithmetic Codes for the Button
The code for the Addition operation is as shown below. The parse statement is used in order to convert the string argument as a signed decimal integer.

The number entered into the EditText textbox is taken from the EditText and stored temporarily in a variable.

Therefore the getText() method can be used to retrieve the data, and the setText() method to display the data. The elswhere.Integer.parseInt() method will throw a “NumberFormatException” error if the String provided is not a proper number.


Similarly, do this for all the other logics to be performed. For each logic, the operation to be performed is clearly stated.


The Equal action button performs according to the required logic. For this, we make use of the if statements. If the user prefers addition, the firstnum and the secondnum will be added and the answer will be stored in the result.


The Clear button is used to remove the previously used values from the text Display. For this the secondnum will be set to 0 and the textField must be set to blank.


Now, Save the file and click the Run button as shown below.


The Jframe panel will appear as shown.


Enter the first number, and second number followed by the logic to be applied. Now click the Equal action button. The result will be displayed on the text Field.
The first number is entered.


The operation to be performed is applied.


Enter the second number.


Now click the Equal sign button, which gives you the result.


The addition operation has been performed successfully. The rest of the arithmetic operations can be performed in a similar manner.

Conclusion: – We hope you enjoyed this informative article on how to create a calculator in Java using Netbeans and will check out our other articles such as Create A Login Page Class Form In Java Using Netbeans or about how to install Netbeans on your computer in detail.

Also, if you would like to learn more about Java, You can try out the “Java Programming Course for Beginner From Scratch” for FREE! The course comes with over 12 hours of video that covers 12 major topics such as Java basics, polymorphism, streams, concurrency and much more.

Also, you can learn to make projects in Java with the “Learn Projects in Java for Beginners from Scratch” online course. The course has 5 hours of video where you will learn to build a media player, games, messenger application and much more in Java.

Leave a comment in case you face any issues and have a code filled day!


  1. This is now become very easy and simple to calculate your life plans and objectives by using the free online calculator, which provides you the basic 5 calculating function of your life, by using this calculator you can calculate your loan, credit, and dues amount which are going to be payable by you in future to settle or make anything in your current life.

  2. This help me some how but
    Try to add a page of source code and some time leave your different account like YouTube where you can add that video

  3. Thank you very much for sharing such valuable and informative blog post about how to learn java programming language. I have read many blogs but here i found something new so great explanation keep posting.

  4. Hello, my calculator keeps crashing I input for example 1+1 then have the program calculate it out. but if i then press clear and begin another calculation the program crashes after i input the new “first” number.


Please enter your comment!
Please enter your name here