Link to home
Start Free TrialLog in
Avatar of purfus
purfus

asked on

really simple java temperature converter

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?
Avatar of Artsemis
Artsemis
Flag of United States of America image

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 :)
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();
Avatar of purfus
purfus

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of StillUnAware
StillUnAware
Flag of Lithuania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
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.
Avatar of purfus

ASKER

Yup, that did the trick.  Makes sense I suppose.  I've been pampered by PHP for too long.  Thanks for all the input.
Don't forget to award his points :)