Temperature Conversion Chart
Hey guys:
I've been struggling all day to make a chart that converts temperatures from Fahrenheit to Celsius. I figured out how to set up the entire program and display it on the console by using the for loop, but my professors wants it to be displayed in a joptionpane. How can you set up a loop within the joptionpane? This is what I have set up, but it just gives me one temperature conversion per dialog box. Any help would be greatly appreciated!!!
package fahrenheitToCelcius;
import javax.swing.JOptionPane;
public class Temperature
{
public static void main(String[ ] args)
{
// Generate values for both temperatures.
for (int fahrenheit = 0; fahrenheit <= 250; fahrenheit += 10)
{
double celsius = (1.8) * (fahrenheit -32);
JOptionPane.showMessageDialog(null, fahrenheit + "F " + " " + celsius + "C\n", "Fahrenheit to Celsius", JOptionPane.INFORMATION_MESSAGE);
}
}
}
Also, I need to have the temperatures below freezing in blue font and the temperatures above boiling in red. How would I do that?
Re: Temperature Conversion Chart
One solution: create a String or StringBuilder prior to your loop, append the data that you're currently displaying to the String or StringBuilder, then after the loop ends, display the String in a JOptionPane.
Re: Temperature Conversion Chart
I am unsure how to set up the StringBuilder. Believe me though, I've been trying for the past hour. Any chance you could be a little more precise?
Re: Temperature Conversion Chart
In this situation, since you're only concatenating 20 or so Strings, just use a String, and simply concatenate in the loop using the + operator.
Code java:
String myString = "";
for (int i = 0; i < someMax; i++) {
String someNewString = // .... create your new String in here
myString += someNewString + "\n"; // concat it to the myString
}
// here show the myString in the JOptionPane
Re: Temperature Conversion Chart
Thank you so much! I finally got it down. You're a lifesaver!