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: NOTE: Result text field is uneditable. Windows Calculator: 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: 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(); 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: 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; 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 NumberThis 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!!!! <!-- s --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="" title="Very Happy" /><!-- s --> Thank you so much, Brandon! |