• Post Reply Bookmark Topic Watch Topic
  • New Topic

Temperature Conversion Project  RSS feed

 
Peter Charuza
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I'm getting a little frustrated with a project I'm working on for class. I've gotten pretty far into it but it seems that every additional step is fighting me.

The problem I'm trying to resolve now is getting my ActionEvent to send the value input in the JTextArea out to be calculated, then to display the result in my result JLabel.

Here is an image of the GUI to give you a better idea of this program is doing.


The basic procedure the teacher is asking for is the user is supposed to enter a temperature in the input. The user then clicks the input scale, and then on the output scale selection the converted value is to be displayed in the output area.

As I mentioned my problem is in the sending of the input value to my calculation area, and getting the value to display in the JLabel.

How should I approach this solution? Do I need to have a listener after the JTextArea of the input box?
Will that allow me to limit the input values to numbers only?


Here is the section of code my ActionListener is:


All the calculations are required to occur in a separete java file, which what I think is tripping me up. How do I send a value from the JTextArea into the Calc.celToFahr method?

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The basic procedure the teacher is asking for is the user is supposed to enter a temperature in the input. The user then clicks the input scale, and then on the output scale selection the converted value is to be displayed in the output area.

If clicking on the output scale is the trigger to calculate the result then your listener needs to be on the group of output RadioButtons although this would be a strange way for the panel to work because after each calculation you would need to clear the selected RadioButton before a new value could be calculated.

All the calculations are required to occur in a separete java file, which what I think is tripping me up. How do I send a value from the JTextArea into the Calc.celToFahr method?

You call the appropriate method of the Calc class ie Calc.celToFahr(myValT0Convert). You will of course need to convert the text input to a double before passing it to the method.
 
Peter Charuza
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tony for the guidance here.

So what I've done so far is get the input text box to display on the output label. I was able to get the input textArea to be converted to a string and then to a double, then back to a string to be displayed.


Output:
98.0
37.0

Looks like it's working great! What a relief!
Now I just need to figure out how to get the result JLabel to display only two decimal points. This is a common question I'm sure, so I'll look for it myself. I'll post how I did it once I get it working.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done.
You might want to look at the String.format(..) method or the java.text.DecimalFormat class
 
Peter Charuza
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It took a little bit to get all the elements working for this project. Thanks for the suggestions, I was reaching that point of peak frustration before it really all clicked.
The decimal format took a little bit to organize properly as I had integrated the double to string conversion within the final output statement.



Here's an excerpt of the code which controls Celsius being converted to Fahrenheit.




With the Decimal Format method is it alright to place that at the very top of the program and reuse the String answer, or should I be creating a new one in each If - Else If statement? I created a new method in each statement just for clarity and to make sure it would work correctly.

Also if I'm using terminology incorrectly, please let me know. I'm really trying to get my theory and nomenclature down right.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DecimalFormat is not synchronized so as long as it will only be used by one thread (which in your case it almost certainly will be) then yes you can create a single formatter and reuse it.

A lot of the code in the snippet you have shown is not relevant to the conversion selection and so should be moved out of the if statement. ie getting the input and converting it to a double will be required regardless of the conversion type so should be before the if statements which decide which conversion to do. The same applies to the code to convert the result back to a double, format and display it which should be after the if statements.

BTW what happens if someone enters 'one' rather than '1' or clicks the conversion without entering any value?

 
Peter Charuza
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh that's good to know about DecimalFormat.

That's a good point, I think in my rush to finish the project and submit it, (it was due yesterday), I left a lot of redundant code in the project. Tony you bring up a great example, as each one of the conversions need the same thing input format, ie. Conversion of the input to String form and then converting that to double format.



That number can then be either converted back to String to be output or sent to our Calc.java file to be converted to the proper form.

Also thank you for bringing up the error handling. I didn't explore that functionality, but it is definitely critical as entering the word One causes the software to crash.

Now that the project is completed I get to see the Professors completed code.
Here is how the Professor's code went about handling a blank or incorrect entery in the JTextArea input box.

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is how the Professor's code went about handling a blank or incorrect entery in the JTextArea input box.

Not sure about that code, I think you may have made a few mistakes in your rewriting it.
Personally I would get the text from the text field and assign it to a local variable and I would have used equals() rather than compareTo().
 
Peter Charuza
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to have to rework this, you're right. With my limited knowledge of Java, the compareTo() method is actually what I was thinking of using as well. I was surprised to see the teacher used something else, his method looked completely new to me. It may have been a typo or I rewrote the wrong section.

I'm going to get this program functioning with full error handling that way I can use this as a resume piece. At least I can use it to chart my progress through Java. I definitely invested a lot of time getting it to where it is now. I'll try to get it reworked by the end of the weekend as best as I can.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!