Solved

really simple java temperature converter

Posted on 2006-07-16
9
626 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
factorial example challenge 10 94
wordmultiple challenge 12 121
Device same like our heart 12 78
iframe detection of parent window scale 20 60
RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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…

867 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

22 Experts available now in Live!

Get 1:1 Help Now