Solved

really simple java temperature converter

Posted on 2006-07-16
9
611 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now