{"id":241,"date":"2017-12-05T06:27:00","date_gmt":"2017-12-05T06:27:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2017\/12\/05\/part-2-how-to-build-a-simple-calculator-in-java-using-netbeans-step-by-step-with-screenshots\/"},"modified":"2020-11-05T13:00:10","modified_gmt":"2020-11-05T12:00:10","slug":"part-2-how-to-build-a-simple-calculator-in-java-using-netbeans-step-by-step-with-screenshots","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/part-2-how-to-build-a-simple-calculator-in-java-using-netbeans-step-by-step-with-screenshots\/","title":{"rendered":"Part 2: How to Build a Simple Calculator in Java Using Netbeans &#8211; Step by Step with Screenshots"},"content":{"rendered":"<div style=\"color: #555555; font-size: 18px; line-height: 30px;\">\n<div style=\"font-family: 'segoe ui';\">\n<div style=\"text-align: justify;\">\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/3.bp.blogspot.com\/-avPVpJC7VGg\/WiY78s2KuDI\/AAAAAAAAAWQ\/-zDxBS5qpVAHTW-AS5L4ceeyvK89b2P1wCLcBGAs\/s1600\/How-To-Build-a-Simple-Calculator-In-Java-Using-Netbeans-Part-2.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"993\" data-original-width=\"1512\" height=\"210\" src=\"https:\/\/3.bp.blogspot.com\/-avPVpJC7VGg\/WiY78s2KuDI\/AAAAAAAAAWQ\/-zDxBS5qpVAHTW-AS5L4ceeyvK89b2P1wCLcBGAs\/s320\/How-To-Build-a-Simple-Calculator-In-Java-Using-Netbeans-Part-2.jpg\" width=\"320\" \/><\/a><\/div>\n<p>This is the Part 2 of How to Build a Simple Calculator in Java using Netbeans IDE. This part follows from the first part. If you have not gone through the first part, you can find it in the link below:<\/p><\/div>\n<p><\/p>\n<div itemprop=\"name\"><span style=\"font-size: small;\"><a href=\"https:\/\/kindsonthegenius.com\/blog\/how-to-build-a-simple-calculator-in-java-using-netbeans-step-by-step-with-screenshots\/\">Part 1: How to Build a Simple Calculator in Java Using Netbeans &#8211; Step by Step with Screenshots&nbsp;<\/a><\/span><\/p>\n<p>Question raised in the first part includes:<br \/>How do I arrange the buttons on the form<br \/>How to I modify the program to handle any number<br \/>How do I handle errors in case of bad input<\/p>\n<p>Let&#8217;s start with the first one<\/p>\n<p><b>Arrangement of the Buttons<\/b><br \/><b>Step 1: <\/b>Right click inside the panel where the buttons are placed. Choose Set Layout &gt; Select Absolute Layout. This is shown in the figure below:<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/3.bp.blogspot.com\/-32Ns5glLE6o\/WiYSSnlK5rI\/AAAAAAAAAVs\/J9CzTDUB_uIpvRnjrlyXy_HyEAZz1oEZwCLcBGAs\/s1600\/Setting%2BLayout.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"767\" data-original-width=\"1311\" height=\"187\" src=\"https:\/\/3.bp.blogspot.com\/-32Ns5glLE6o\/WiYSSnlK5rI\/AAAAAAAAAVs\/J9CzTDUB_uIpvRnjrlyXy_HyEAZz1oEZwCLcBGAs\/s320\/Setting%2BLayout.jpg\" width=\"320\" \/><\/a><\/div>\n<p>If you have done this correctly, then the buttons will no longer snap.<\/p>\n<p><b>Step 2: <\/b>Take time to resize and arrange your buttons as shown in the screenshot<\/p>\n<p><b>Step 3(Optional):<\/b> To get a very uniform look, click a buttons and look at the properties. Find the Layout section as shown.<\/p>\n<p><\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/2.bp.blogspot.com\/-rdp6q04SHgc\/WiYTsDu1mnI\/AAAAAAAAAV8\/vLqJP5SQibAaagMmYNF2Rik1uswtPpSHgCLcBGAs\/s1600\/Button%2BLayout.JPG\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"202\" data-original-width=\"346\" height=\"186\" src=\"https:\/\/2.bp.blogspot.com\/-rdp6q04SHgc\/WiYTsDu1mnI\/AAAAAAAAAV8\/vLqJP5SQibAaagMmYNF2Rik1uswtPpSHgCLcBGAs\/s320\/Button%2BLayout.JPG\" width=\"320\" \/><\/a><\/div>\n<p><\/p>\n<ul>\n<li>make sure all the buttons except the equals button, has the same width and height<\/li>\n<li>Make sure all buttons in the same row have the same Y<\/li>\n<li>All buttons on the in the same column have the same X<\/li>\n<\/ul>\n<p>If you have done everything correctly, your form would be looking really good.<br \/>Lets now look at the next topic.<\/p>\n<p><b>How to Handle any Number<\/b><br \/>To do this you need to replace the codes for the buttons with a slightly modified code.<br \/>Modify Code for Button 0 &#8211; 9 with the following codes:<\/p>\n<p><b><i>For Button 1<\/i><\/b><br \/><span style=\"font-size: x-small;\"><span style=\"color: #351c75;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String res = txtResult.getText();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(txtResult.getText().isEmpty())<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText(&#8220;1&#8221;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if(res.contains(&#8220;+&#8221;) || res.contains(&#8220;-&#8220;) || res.contains(&#8220;*&#8221;) || res.contains(&#8220;\/&#8221;))<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText(txtResult.getText() + &#8220;1&#8221;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value2 = Integer.parseInt(value2 + &#8220;1&#8221;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText(txtResult.getText()+ &#8220;1&#8221;);<\/span><\/span><\/p>\n<p><i><b>For Button 2<\/b><\/i><br \/><span style=\"font-size: x-small;\"><span style=\"color: #0b5394;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String res = txtResult.getText();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(txtResult.getText().isEmpty())<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText(&#8220;2&#8221;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if(res.contains(&#8220;+&#8221;) || res.contains(&#8220;-&#8220;) || res.contains(&#8220;*&#8221;) || res.contains(&#8220;\/&#8221;))<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText(txtResult.getText() + &#8220;2&#8221;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value2 = Integer.parseInt(value2 + &#8220;2&#8221;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText(txtResult.getText()+ &#8220;2&#8221;);<\/span><\/span><\/p>\n<p>Continue with the same trend for buttons 3, 4, 5, 6, 7,&nbsp; 9, 0. You can do this by replacing the places you have the numbers<\/p>\n<p><b>Code for +, -, * and \/ Buttons<\/b><br \/>Also modify the codes for the plus, minus, division and multiplication buttons as follows:<\/p>\n<p>For plus button, use the code:<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(txtResult.getText().isEmpty())<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value1 = Integer.parseInt(txtResult.getText());<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText( txtResult.getText() + &#8221; &#8221; + btnPlus.getText() ); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; operator = &#8220;plus&#8221;;<\/p>\n<p>For the minus button, use the code:<br \/><span style=\"color: #0b5394;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(txtResult.getText().isEmpty())<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value1 = Integer.parseInt(txtResult.getText());<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText( txtResult.getText() + &#8221; &#8221; + btnMinus.getText() ); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; operator = &#8220;minus&#8221;;<\/span><\/p>\n<p>For the division button, use the code<br \/><span style=\"color: #0b5394;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(txtResult.getText().isEmpty())<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value1 = Integer.parseInt(txtResult.getText());<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText( txtResult.getText() + &#8221; &#8221; + btnDivide.getText() ); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; operator = &#8220;division&#8221;;<\/span><\/p>\n<p>For the multiplication button, use the code:<br \/><span style=\"color: #0b5394;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(txtResult.getText().isEmpty())<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value1 = Integer.parseInt(txtResult.getText());<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText( txtResult.getText() + &#8221; &#8221; + btnMultiplication.getText() ); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; operator = &#8220;multiplication&#8221;;<\/span><\/p>\n<p><b>Test the Program<\/b><br \/>Now run the program and perform some operation to see the result. Your program should be working well by now. But there are some questions that are yet to be answered. For example:<\/p>\n<ul>\n<li>How to we handle decimal points<\/li>\n<li>How to we handle wrong inputs<\/li>\n<li>If you have any more questions, leave it in the comment box below. <\/li>\n<\/ul>\n<p>We would answer these questions in Part 3.<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This is the Part 2 of How to Build a Simple Calculator in Java using Netbeans IDE. This part follows from the first part. If &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[85],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/241"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=241"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/241\/revisions"}],"predecessor-version":[{"id":1690,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/241\/revisions\/1690"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}