Solved

really simple java temperature converter

Posted on 2006-07-16
9
644 Views
Last Modified: 2012-05-05
Ok, I'm brand new to Java so I'm sure this is a simple problem.  I tried making a really simple temp converter just to play with the language and it's not been so simple.  Bellow is the code for the button actions.

      public void actionPerformed(ActionEvent e) {
            if (e.getSource() == button1) {
                  float tempc = Float.parseFloat(textfield1.getText());
                  label1.setText(tempc + " degrees celsius = " + (((9/5)*tempc)+32) + " degrees ferhenheight");
            }
            else if(e.getSource() == button2) {
                  float tempf = Float.parseFloat(textfield1.getText());
                  label1.setText(tempf + " degrees ferhenheight = " + ((tempf - 32)*(5/9)) + " degrees celsius");
            }
            else if(e.getSource() == button3) {
                  System.exit(0);
            }
      }

Pretty straight forward.  As far as I know the math is right.  The formula looks right but it sure doesn't evaluate correctly.  According to my program 20c = 52f.  But in this dimension 20c = 68f.  Also the output for converting to c (button2) always shows 0.0 for the calculated value.  So something is fishy.  Anyone know why this thing is so illogical in it's assertions?
0
Comment
Question by:purfus
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 1

Expert Comment

by:Artsemis
ID: 17119513
the reason the second button isn't working correctly is because you copy and pasted label1 and textfield1 where it should be label2 and textfield2 into the second statement :)
0
 
LVL 1

Expert Comment

by:Artsemis
ID: 17119523
As for the values being incorrect, try using tempc.trim() on the string before getting the value of it, or use valueOf() like this:
Float tempc = Float.valueOf(tempc.trim()).floatValue();
0
 
LVL 3

Author Comment

by:purfus
ID: 17119573
Hang on.  Now I'm more confused...  The textfield and label are supposed to be 1 and 1 because I'm only reading from one textfield and writing to one label.  Basically I have a label, then a text box, then 3 buttons.

Float tempc = Float.valueOf(tempc.trim()).floatValue();

I dont understand the declaration above.  At tempc.trim() tempc is undefined.  trim is not a function of textfield1 according to eclipse so where does the textfield value come from?  Is valueOf a better parser than parsefloat?

I'm not sure the problem is there because the program outputs to the label just fine, it's just the calculated values that are off.  So if I put in 32 and click button 1 it puts "32.0 degrees celsius = 64.0 degrees ferhenheight" in the label.  Which tells me its getting the textfield value just fine but the calculation is all off.  So (((9/5)*tempc)+32)  that isn't evaluating correctly.  The same for button 2 except it always evaluates to 0.0.  So if I put in 32 it's correct but...  Say I put in 54 and hit button2 it puts "54 degrees ferhenheight = 0.0 degrees celsius".  Which again tells me that ((tempf - 32)*(5/9)) that is not evaluating correctly.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 14

Accepted Solution

by:
StillUnAware earned 500 total points
ID: 17119719
You should use at explicitly float numbers, when converting the values:

(((9/5)*tempc)+32)  ->  (((9.0/5.0*tempc)+32.0)
((tempf - 32)*(5/9))  ->  ((tempf - 32.0)*(5.0/9.0))

that's because the (5/9) uses the integers and it equals to 0, and (9/5) equals to 1.
0
 
LVL 2

Expert Comment

by:azoapes
ID: 17120647
In C# you have to enter the decimal number (in your textfield) with a comma --> ,

Try entering a comma (,) instead of dot (.) in the textfield, or the parser may experience problems. But I might as well be wrong on that one.

You said you got 64 by entering 32. 64 is 2 * 32 or 32 + 32, and would truthfully respond to 32 + 32 where (9/5) * tempc = 32 and 32 = 32

Therefore, place a breakpoint and watch the value tempc as it flows. Set a constant value to tempc and see if the math does alright. You can try splitting the operations into several parts (declare a new float for each operation), like this:

float tempc = Float.parseFloat(textfield1.getText());
double nineFifths = 9/5;
double tempf = tempc;
tempf = tempf * nineFifths;
tempf += 32;
label1.setText(tempc + " degrees celsius = " + tempf + " degrees ferhenheight");

or try enormously unneccassary float explicition, something like this:

( ( ( 9/5 ) * tempc ) + 32 )  -->  ( ( (float)(9/5) * (float)(tempc) ) + (float)(32.0) )

or typecasting double and explicit (PROBABLY SOLVES PROBLEM):

double tempc = Double.parseDouble(textfield1.getText());
double nineFifths = (double)(9/5);
double tempf = (double)(tempc);
tempf = (double) ((double)(tempf) * (double)(nineFifths)); // remove the two inside (double) ?
tempf += (double)(32.0);
label1.setText(tempc.toString() + " degrees celsius = " + tempf.toString() + " degrees ferhenheight");

/Simeon
0
 
LVL 2

Expert Comment

by:azoapes
ID: 17120653
Sorry, the first code paragraph there doesn't, of course, work. It was just an example. The last paragraph should work fine though, and replaces the upper one.
0
 
LVL 1

Expert Comment

by:Artsemis
ID: 17122079
My apologies, I made a bad assumption that you had seperate text fields :)  StillUnAware is correct, you simply need to put .0 on the end of your hard-coded numbers so they don't mess up float calculations.
0
 
LVL 3

Author Comment

by:purfus
ID: 17151303
Yup, that did the trick.  Makes sense I suppose.  I've been pampered by PHP for too long.  Thanks for all the input.
0
 
LVL 1

Expert Comment

by:Artsemis
ID: 17151351
Don't forget to award his points :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question