Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Temperature Conversion Chart

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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.

    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

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Temperature Conversion Chart

    Thank you so much! I finally got it down. You're a lifesaver!

Similar Threads

  1. Temperature Converting Application
    By mlenarto199 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2012, 07:38 PM
  2. Temperature Table Celsius and Fahrenheit
    By Krodfish in forum Loops & Control Statements
    Replies: 2
    Last Post: February 2nd, 2012, 10:06 PM
  3. (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)
    By skw4712 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: July 18th, 2011, 07:34 PM
  4. NEED HELP WITH TEMPERATURE
    By Dracer in forum AWT / Java Swing
    Replies: 5
    Last Post: December 11th, 2010, 03:12 PM
  5. Converting temperature program
    By ixjaybeexi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2009, 07:27 PM

Tags for this Thread