Results 1 to 8 of 8
  1. #1
    New2J@v@ is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Simple java conversion driving me crazy!!

    Hello everyone. I have to code a program to convert Farenheit to Celcius with a simple formula. The object is to collect input from the user via a JTextField and display the output in a JLabel... My code is below... any additional input would be greatly appreciated! I did not supply the test class but I can if needed.

    Java Code:
    package WindowBuilderTest;
    
    //import java.awt.BorderLayout;
    //import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    public class Demo extends JFrame 
    {
    
    	
    	private JPanel contentPane;
    	private JTextField textField;
    	private double farenheit;
    	private double celciusConv;
    	private JLabel celciusLabel;
    
    	
    	
    	public Demo() 
    	{
    		
    		
    		super("Convert Farenheit to Celcius");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		
    		celciusLabel = new JLabel();
    		celciusLabel.setBounds(78, 55, 180, 14);
    		contentPane.add(celciusLabel);
    		
    		textField = new JTextField();
    		textField.setText("Enter Farenheit Temp");
    		textField.setBounds(127, 148, 150, 20);
    		contentPane.add(textField);
    		textField.setColumns(10);
    		
    		textField.selectAll();
    		
    		JButton btnConvert = new JButton("Convert");
    		btnConvert.addActionListener(new ActionListener() 
    		{
    			public void actionPerformed(ActionEvent arg0) 
    			{
    				calcTemp();
    				
    				celciusLabel.setText(String.format("Degrees in Celcius = %.4f", celciusConv));	
    			}
    			
    
    		});	
    		
    		btnConvert.setBounds(312, 147, 89, 23);
    		contentPane.add(btnConvert);
    	} //
    	
    	public void calcTemp()
    	{
    		celciusConv = ( farenheit - 32) * 5 / 9;
    	}
    	
    	{
    	}
    
    }
    Last edited by JosAH; 06-12-2012 at 08:09 PM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Simple java conversion driving me crazy!!

    Now we know what you're code is supposed to do; did you have a question?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    New2J@v@ is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: Simple java conversion driving me crazy!!

    The conversion is not working properly. If a value of 100 is entered, it returns -17 when the output should be 37.777, based on my output format. However, after clicking the ok button it doesn't work.

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Simple java conversion driving me crazy!!

    Where in the program are you setting the value for your farenheit variable?
    I believe that is your biggest issue.
    Last edited by sehudson; 06-12-2012 at 08:26 PM.

  5. #5
    New2J@v@ is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: Simple java conversion driving me crazy!!

    As I stated previously, there is a test class to display the jframe. Here is the code for that class.

    package WindowBuilderTest;

    import java.awt.EventQueue;


    public class JFrame
    {

    public static void main(String[] args)
    {




    EventQueue.invokeLater(new Runnable()
    {
    public void run()
    {
    try {
    Demo frame = new Demo();
    frame.setSize(500, 300);
    frame.setVisible(true);
    } catch (Exception e)

    {
    e.printStackTrace();
    }
    }
    });
    }


    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Simple java conversion driving me crazy!!

    Quote Originally Posted by New2J@v@ View Post
    The conversion is not working properly. If a value of 100 is entered, it returns -17 when the output should be 37.777, based on my output format. However, after clicking the ok button it doesn't work.
    In method calcTemp(), print the value of variable fahrenheit and see if it contains the value you expected. You can use a simply System.out.println( ... ) so the value will be printed on your console.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Simple java conversion driving me crazy!!

    Again, take a look at your Demo class. You declared your double variable farenheit, but where in the program are you setting its value?

  8. #8
    New2J@v@ is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: Simple java conversion driving me crazy!!

    That was the key! As usual, I instantly thought something larger was wrong, but after you point that out I realized I did not have a get statement to pull in the input value. After doing that she worked like a champ.
    Thanks again!

Similar Threads

  1. Driving me crazy!!!! Please help!!
    By Quizzle23 in forum New To Java
    Replies: 33
    Last Post: 02-24-2011, 08:00 AM
  2. The null message is driving me crazy
    By Yakg in forum New To Java
    Replies: 5
    Last Post: 12-02-2010, 07:03 PM
  3. image does not refresh-driving me crazy
    By jambon in forum AWT / Swing
    Replies: 1
    Last Post: 04-09-2010, 04:25 PM
  4. THIS PROGRAM IS DRIVING ME CRAZY!!! help fixing it
    By syntrax in forum New To Java
    Replies: 2
    Last Post: 12-18-2009, 04:27 AM
  5. Errors driving me crazy! although compiles fine
    By irishsea2828 in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 03:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •