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 4 of 4

Thread: Trouble with fahrenheit to celsius conversion.

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Location
    Denmark
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Trouble with fahrenheit to celsius conversion.

    Hi there, first post on this board.

    I'm just starting out with learning java, and i'm currently fiddling with the standard tutorial.

    I did the one about the celsius -> fahrenheit converter, and thought i'd make it on better by adding the other way round, but it just won't work

    Any suggestions are warmly welcomed.

    Here's the source code from netbeans IDE:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package Test;
     
    /**
     *
     * @author Shitney Lee
     */
    public class TestAppGUI extends javax.swing.JFrame {
     
        /**
         * Creates new form TestAppGUI
         */
        public TestAppGUI() {
            initComponents();
        }
     
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            tempTextField1 = new javax.swing.JTextField();
            convertButton1 = new javax.swing.JButton();
            celsiusLabel1 = new javax.swing.JLabel();
            fahrenheitLabel1 = new javax.swing.JLabel();
            fahrenheitLabel2 = new javax.swing.JLabel();
            celsiusLabel2 = new javax.swing.JLabel();
            convertButton2 = new javax.swing.JButton();
            tempTextField2 = new javax.swing.JTextField();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Temperature Converter");
     
            tempTextField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField1ActionPerformed(evt);
                }
            });
     
            convertButton1.setText("Convert!");
            convertButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    convertButton1ActionPerformed(evt);
                }
            });
     
            celsiusLabel1.setText("Celsius");
     
            fahrenheitLabel1.setText("Fahrenheit");
     
            fahrenheitLabel2.setText("Fahrenheit");
     
            celsiusLabel2.setText("Celsius");
     
            convertButton2.setText("jButton1");
            convertButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    convertButton2ActionPerformed(evt);
                }
            });
     
            tempTextField2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField2ActionPerformed(evt);
                }
            });
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(tempTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(convertButton1)
                        .addComponent(convertButton2)
                        .addComponent(tempTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(31, 31, 31)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(celsiusLabel2)
                        .addComponent(fahrenheitLabel2)
                        .addComponent(celsiusLabel1)
                        .addComponent(fahrenheitLabel1))
                    .addContainerGap(160, Short.MAX_VALUE))
            );
     
            layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {convertButton1, tempTextField1});
     
            layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {convertButton2, tempTextField2});
     
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(celsiusLabel1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(convertButton1)
                        .addComponent(fahrenheitLabel1))
                    .addGap(29, 29, 29)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(fahrenheitLabel2)
                        .addComponent(tempTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(celsiusLabel2)
                        .addComponent(convertButton2))
                    .addContainerGap(74, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>
     
        private void tempTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
        }
     
        private void convertButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        int tempFahr1;
            tempFahr1 = (int)((Double.parseDouble(tempTextField1.getText()))
                * 1.8 + 32);
        fahrenheitLabel1.setText(tempFahr1 + " Fahrenheit");
        }
     
        private void tempTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
     
        }
     
        private void convertButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        int tempCels2;
            tempCels2 = (int)((Double.parseDouble(tempTextField2.getText()))
                    - 32 * 5.0 / 9.0) ;
        celsiusLabel2.setText(tempCels2 + " Celsius");
        }
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(TestAppGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(TestAppGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(TestAppGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(TestAppGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TestAppGUI().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JLabel celsiusLabel1;
        private javax.swing.JLabel celsiusLabel2;
        private javax.swing.JButton convertButton1;
        private javax.swing.JButton convertButton2;
        private javax.swing.JLabel fahrenheitLabel1;
        private javax.swing.JLabel fahrenheitLabel2;
        private javax.swing.JTextField tempTextField1;
        private javax.swing.JTextField tempTextField2;
        // End of variables declaration
    }


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Trouble with fahrenheit to celsius conversion.

    Why doesn't it work? It works, your algorithm is just wrong.
    This should work:
    private void convertButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    		int tempCels2;
    		tempCels2 = (int) ((Double.parseDouble(tempTextField2.getText()) - 32) / 1.8);
    		celsiusLabel2.setText(tempCels2 + " Celsius");
    	}

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Trouble with fahrenheit to celsius conversion.

    Quote Originally Posted by HaHom Tsa Pfnuma View Post
    ...
    I did the one about the celsius -> fahrenheit converter, and thought i'd make it on better by adding the other way round
    That's a Great Idea! Start with something that works and make changes to make sure you understand what's going on. I like that approach.

    Quote Originally Posted by HaHom Tsa Pfnuma
    won't work...suggestions
    Inspect the statements that control the displayed values. The program does the hard part (getting user input, calculating a result based on that value, and then displaying the result). All you have to do is to make sure the calculations are correct. That's just a matter of math.

    Anyhow...


    So, the program converts Celsius to Fahrenheit correctly, right? Here's the formula.


    DegreesF = DegreesC * 9.0/5.0 + 32.0

    Compare it with the program statement that does the conversion. So, far, so good, right?


    Now, solve (algebraically) for the other direction:


    DegreesC = (DegreesF - 32.0) * 5.0/9.0

    Compare this with your corresponding program statement.

    Cheers!

    Z

  4. The Following User Says Thank You to Zaphod_b For This Useful Post:

    HaHom Tsa Pfnuma (July 27th, 2012)

  5. #4
    Junior Member
    Join Date
    Jul 2012
    Location
    Denmark
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with fahrenheit to celsius conversion.

    Aha, the missing parenthesisses (if that's how you say it in plural, anyway, heh)!!! I've got to look closer at what i'm doing.

    Thanks a lot, especially for the detailed explanation. I really appreciate you taking your time to explain it properly instead of just providing the solution; also gives a nice feeling to actually solve it by myself.

Similar Threads

  1. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 01:15 PM
  2. Help with conversion
    By TheEvilCouncil in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2012, 08:29 PM
  3. Won't display calculated fahrenheit and celsius when running.
    By smithmar in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 17th, 2012, 09:00 PM
  4. Temperature Table Celsius and Fahrenheit
    By Krodfish in forum Loops & Control Statements
    Replies: 2
    Last Post: February 2nd, 2012, 10:06 PM
  5. Conversions Between Celsius and Fahrenheit
    By baueml01 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 9th, 2011, 08:49 PM