BP Forums
Basic Calculator Issue - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: Java (https://bpforums.info/forumdisplay.php?fid=19)
+---- Thread: Basic Calculator Issue (/showthread.php?tid=558)



Basic Calculator Issue - Vinwarez - 07-10-2012

The first thing you try to make when you learn a new computer language is a basic calculator, right? Well, I've made many calculators and similar "softwares", but I've always had a small "error".

I set up the window with three text fields, two labels, two buttons and a list. I, also, declared three integer variables; n1, n2 and res.

Right now, you're probably like "Alright.. I cannot see a problem here", well we all know that four divided by two equals two, right? But what happens when you divide four by three? You get the result of one point three. Well, on my calculator it shows only one.

My Calculator:
[Image: scaled.php?server=687&filename=kalkulato...es=landing]
NOTE: Result text field is uneditable.


Windows Calculator:
[Image: scaled.php?server=841&filename=windowsca...es=landing]

I assume you will tell me "Just change integers to doubles", but I don't want that. I want it to display any decimals except zero. Example:
4 / 2 = 2(.0) | Don't display ".0"
4 / 3 = 1(.3) | Display ".3"

The help would be highly appreciated.


Re: Basic Calculator Issue - brandonio21 - 07-10-2012

Well, there is one easy solution to this, and as you probably predicted: You need to change the integers to doubles. Although doubles give you ".0" and you don't want that, you simply need to use doubles in order to keep track of decimals. By using integers in a calculator, your calculator is only correct when doing math with no remainders or decimals.

Vinwarez Wrote:I assume you will tell me "Just change integers to doubles", but I don't want that. I want it to display any decimals except zero. Example:
4 / 2 = 2(.0) | Don't display ".0"
4 / 3 = 1(.3) | Display ".3"

In order to solve this problem, you're going to want to use a NumberFormatter. So, for example, say your answer double is res. You then need to use the following code segment:
Code:
NumberFormat df = DecimalFormat.getInstance();
df.setMinimumFractionDigits(0);
df.setRoundingMode(RoundingMode.DOWN);

String answer = df.format(res); //This is the new //formatted variable that doesn't include the .0 at the
//end of it

So now all you have to do is fill the textbox with the String answer, and you're good to go!

Edit: I have just tested this and it does indeed work: 4/2 displays "2", while 4/3 displays "1.333"


Re: Basic Calculator Issue - Vinwarez - 07-10-2012

Thank you for the code snippet, but I am still confused. I don't know if I copied it on the wrong place or if it's something else.

Here is the screenshot of the code snippet on my Eclipse:
[Image: scaled.php?server=843&filename=errormsg0...es=landing]


Re: Basic Calculator Issue - brandonio21 - 07-10-2012

That's right! Now all you need to do is import the libraries!

Code:
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;

Add that to the very top of your code and you should be good!


Re: Basic Calculator Issue - Vinwarez - 07-11-2012

Quote:Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot format given Object as a Number
at java.text.DecimalFormat.format(Unknown Source)
at java.text.Format.format(Unknown Source)
at KalkulatorInt$thehandler.actionPerformed(KalkulatorInt.java:144)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
This is the error I get whenever I try to calculate any number using any mathematical operation.

Maybe it would be easier to help if you take a look at the code. The problem seems to be on the line 147.


Re: Basic Calculator Issue - brandonio21 - 07-11-2012

Alright, I see the problem.

What you're going to want to do is change this line:
Code:
String answer = df.format(res);

To this:
Code:
String answer = df.format(product);

This is because what you were trying to do was format the TextField as a String. However, you can't make that conversion. So, we need to format the answer (product) as a String, and then display it in the textfield.


Re: Basic Calculator Issue - Vinwarez - 07-11-2012

It works! It actually works!!!! <!-- sBig Grin --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="Big Grin" title="Very Happy" /><!-- sBig Grin -->
Thank you so much, Brandon!