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

Thread: GUI temp converter program troubles

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default GUI temp converter program troubles

    Program seems to be sending the wrong conversions when a temp is entered in the kelvin field. For some reasons changes the kelvin temp value, and also seems to be crashing on random runs when value entered in kelvin field. Also does not return InvalidTemperatureException for the kelvin field. Have tried numerous different things with no avail, any suggestions would be appreciated. Thanks!

    public class TempConverter_C_C extends JFrame
    {	
    	double firstValue = 0.0;
    	JPanel topPanel, bottomPanel;
    	JLabel label1, label2, label3;
    	JTextField fahrenheitValueField, celsiusValueField, kelvinValueField;
    	JButton convertButton, clearButton;
     
    	//constructor
    	TempConverter_C_C()
    	{
    		super("Temp App");
    		this.setSize(400, 400);
    		this.setLayout(new GridLayout(2,0) );//anonymous layout object
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setLocationRelativeTo(null);
     
    		//two JPanels
    		topPanel = new JPanel();
    		bottomPanel = new JPanel();
     
    		//set panel layouts
    		topPanel.setLayout(new GridLayout(3,2) );
    		bottomPanel.setLayout(new FlowLayout() );
     
    		//set background colors
    		topPanel.setBackground(Color.BLUE);
    		bottomPanel.setBackground(Color.RED);
     
    		//add panels to the frame
    		this.add(topPanel);
    		this.add(bottomPanel);
     
    		//add some components to each panel
    		label1 = new JLabel("Fahrenheit: ");
    		//add it to top panel
    		topPanel.add(label1);
     
    		//add the field for input
    		fahrenheitValueField = new JTextField();
    		topPanel.add(fahrenheitValueField);
    		fahrenheitValueField.addFocusListener(new ButtonHandler());
    		fahrenheitValueField.addActionListener(new ButtonHandler());
     
    		label2 = new JLabel("Celsius: ");
    		//add it to top panel
    		topPanel.add(label2);
     
    		//add the field for input
    		celsiusValueField = new JTextField();
    		celsiusValueField.addActionListener(new ButtonHandler());
    		celsiusValueField.addFocusListener(new ButtonHandler());
    		topPanel.add(celsiusValueField);
     
    		label3 = new JLabel("Kelvin: ");
    		//add it to top panel
    		topPanel.add(label3);
     
    		//add the field for input
    		kelvinValueField = new JTextField();
    		kelvinValueField.addActionListener(new ButtonHandler());
    		kelvinValueField.addFocusListener(new ButtonHandler());
    		topPanel.add(kelvinValueField);
     
    		this.setVisible(true);
    	}//end constructor
     
    	public static void main(String[] args)
    	{
    		TempConverter_C_C obj = new TempConverter_C_C();
    	}//end main
     
    	//implements TempConversionMethods and ActionListener
    	private class ButtonHandler implements ActionListener, TempConversionMethods, FocusListener
    	{
     
    		//implement the actionPerformed method
    		public void actionPerformed(ActionEvent e)
    		{
    			if(e.getSource().equals(fahrenheitValueField))
    			{
    				String firstValueString = fahrenheitValueField.getText();
    				//parse it to a double
    				firstValue = Double.parseDouble(firstValueString);
     
    				try {
    					if(firstValue < -459.67)	
    					{
    						throw new InvalidTemperatureException(firstValue);
    					}
    					celsiusValueField.setText("" + fahrenheitToCelsius(firstValue));
    					kelvinValueField.setText("" + fahrenheitToKelvin(firstValue));
    					}//end try
    				catch(InvalidTemperatureException ex)
    				{
    					JOptionPane.showMessageDialog(topPanel, "InvalidTemperatureException:" +
    												" the entered temperature of " + firstValue + " is below absolute zero");
    					fahrenheitValueField.setText("");
    					fahrenheitValueField.requestFocus();
    				}//end catch
    			}//end if
     
    			if(e.getSource().equals(celsiusValueField));
    			{
    				String firstValueString = celsiusValueField.getText();
    				//parse it to a double
    				firstValue = Double.parseDouble(firstValueString);
     
    				try {
    					if(firstValue < -273.15)	
    					{
    						throw new InvalidTemperatureException(firstValue);
    					}
    					fahrenheitValueField.setText("" + celsiusToFahrenheit(firstValue));
    					kelvinValueField.setText("" + celsiusToKelvin(firstValue));
    					}//end try
    				catch(InvalidTemperatureException ex)
    				{
    					JOptionPane.showMessageDialog(topPanel, "InvalidTemperatureException:" +
    												" the entered temperature of " + firstValue + " is below absolute zero");
    					celsiusValueField.setText("");
    					celsiusValueField.requestFocus();
    				}//end catch
    			}//end if
     
    			if(e.getSource().equals(kelvinValueField))
    			{
    				String firstValueString = kelvinValueField.getText();
    				//parse it to a double
    				firstValue = Double.parseDouble(firstValueString);
     
    				try {
    					if(firstValue < 0.0)	
    					{
    						throw new InvalidTemperatureException(firstValue);
    					}
    					celsiusValueField.setText("" + kelvinToCelsius(firstValue));
    					fahrenheitValueField.setText("" + kelvinToFahrenheit(firstValue));
    				}//end try
    				catch(InvalidTemperatureException ex)
    				{
    					JOptionPane.showMessageDialog(topPanel, "InvalidTemperatureException:" +
    												" the entered temperature of " + firstValue + " is below absolute zero");
    					kelvinValueField.setText("");
    					kelvinValueField.requestFocus();
    				}//end catch
    			}//end if
    		}//end actionPerformed
     
    		public double celsiusToKelvin(double temp) {
    			return temp + 273.15;
    		}//end method
     
    		public double kelvinToCelsius(double temp) {
    			return temp - 273.15;
    		}//end method
     
    		public double celsiusToFahrenheit(double temp) {
    			return 9.0/5.0 * temp + 32;
    		}//end method
     
    		public double fahrenheitToCelsius(double temp) {
    			return 5.0/9.0 * (temp - 32);
    		}//end method
     
    		public double fahrenheitToKelvin(double temp) {
    			fahrenheitToCelsius(temp);
    			return celsiusToKelvin(temp);
     
    		}//end method
     
    		public double kelvinToFahrenheit(double temp) {
    			kelvinToCelsius(temp);
    			return celsiusToFahrenheit(temp);
     
    		}//end method
     
    		public void focusGained(FocusEvent f) {
    			if(f.getSource().equals(fahrenheitValueField))
    			{
    				fahrenheitValueField.setText("");
    			}
     
    			if(f.getSource().equals(celsiusValueField))
    			{
    				celsiusValueField.setText("");
    			}
     
    			if(f.getSource().equals(kelvinValueField))
    			{
    				kelvinValueField.setText("");
    			}	
    		}//end method
     
    		public void focusLost(FocusEvent arg0) {
     
    		}//end method
    	}//end inner class+
     
    	public class InvalidTemperatureException extends Exception
    	{
    		//constructor
    		InvalidTemperatureException(double t)
    		{
    			//message to be passed up to exception class
    			super("Invalid temperature value of " + t + " was entered");
    			firstValue = t;
     
    		}//end method
    	}//end class
    }//end class


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,571
    Thanks
    49
    Thanked 2,232 Times in 2,204 Posts

    Default Re: GUI temp converter program troubles

    seems to be crashing on random run
    Please copy and paste here the full text of the error messages.

    Add the import statements for your code so it can be copied and compiled.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: GUI temp converter program troubles

    here are the import statements:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    heres all the code again including the import statements so it's just one copy and paste:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class TempConverter_C_C extends JFrame
    {	
    	double firstValue = 0.0;
    	JPanel topPanel, bottomPanel;
    	JLabel label1, label2, label3;
    	JTextField fahrenheitValueField, celsiusValueField, kelvinValueField;
    	JButton convertButton, clearButton;
     
    	//constructor
    	TempConverter_C_C()
    	{
    		super("Temp App");
    		this.setSize(400, 400);
    		this.setLayout(new GridLayout(2,0) );//anonymous layout object
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setLocationRelativeTo(null);
     
    		//two JPanels
    		topPanel = new JPanel();
    		bottomPanel = new JPanel();
     
    		//set panel layouts
    		topPanel.setLayout(new GridLayout(3,2) );
    		bottomPanel.setLayout(new FlowLayout() );
     
    		//set background colors
    		topPanel.setBackground(Color.BLUE);
    		bottomPanel.setBackground(Color.RED);
     
    		//add panels to the frame
    		this.add(topPanel);
    		this.add(bottomPanel);
     
    		//add some components to each panel
    		label1 = new JLabel("Fahrenheit: ");
    		//add it to top panel
    		topPanel.add(label1);
     
    		//add the field for input
    		fahrenheitValueField = new JTextField();
    		topPanel.add(fahrenheitValueField);
    		fahrenheitValueField.addFocusListener(new ButtonHandler());
    		fahrenheitValueField.addActionListener(new ButtonHandler());
     
    		label2 = new JLabel("Celsius: ");
    		//add it to top panel
    		topPanel.add(label2);
     
    		//add the field for input
    		celsiusValueField = new JTextField();
    		celsiusValueField.addActionListener(new ButtonHandler());
    		celsiusValueField.addFocusListener(new ButtonHandler());
    		topPanel.add(celsiusValueField);
     
    		label3 = new JLabel("Kelvin: ");
    		//add it to top panel
    		topPanel.add(label3);
     
    		//add the field for input
    		kelvinValueField = new JTextField();
    		kelvinValueField.addActionListener(new ButtonHandler());
    		kelvinValueField.addFocusListener(new ButtonHandler());
    		topPanel.add(kelvinValueField);
     
    		this.setVisible(true);
    	}//end constructor
     
    	public static void main(String[] args)
    	{
    		TempConverter_C_C obj = new TempConverter_C_C();
    	}//end main
     
    	//implements TempConversionMethods and ActionListener
    	private class ButtonHandler implements ActionListener, TempConversionMethods, FocusListener
    	{
     
    		//implement the actionPerformed method
    		public void actionPerformed(ActionEvent e)
    		{
    			if(e.getSource().equals(fahrenheitValueField))
    			{
    				String firstValueString = fahrenheitValueField.getText();
    				//parse it to a double
    				firstValue = Double.parseDouble(firstValueString);
     
    				try {
    					if(firstValue < -459.67)	
    					{
    						throw new InvalidTemperatureException(firstValue);
    					}
    					celsiusValueField.setText("" + fahrenheitToCelsius(firstValue));
    					kelvinValueField.setText("" + fahrenheitToKelvin(firstValue));
    					}//end try
    				catch(InvalidTemperatureException ex)
    				{
    					JOptionPane.showMessageDialog(topPanel, "InvalidTemperatureException:" +
    												" the entered temperature of " + firstValue + " is below absolute zero");
    					fahrenheitValueField.setText("");
    					fahrenheitValueField.requestFocus();
    				}//end catch
    			}//end if
     
    			if(e.getSource().equals(celsiusValueField));
    			{
    				String firstValueString = celsiusValueField.getText();
    				//parse it to a double
    				firstValue = Double.parseDouble(firstValueString);
     
    				try {
    					if(firstValue < -273.15)	
    					{
    						throw new InvalidTemperatureException(firstValue);
    					}
    					fahrenheitValueField.setText("" + celsiusToFahrenheit(firstValue));
    					kelvinValueField.setText("" + celsiusToKelvin(firstValue));
    					}//end try
    				catch(InvalidTemperatureException ex)
    				{
    					JOptionPane.showMessageDialog(topPanel, "InvalidTemperatureException:" +
    												" the entered temperature of " + firstValue + " is below absolute zero");
    					celsiusValueField.setText("");
    					celsiusValueField.requestFocus();
    				}//end catch
    			}//end if
     
    			if(e.getSource().equals(kelvinValueField))
    			{
    				String firstValueString = kelvinValueField.getText();
    				//parse it to a double
    				firstValue = Double.parseDouble(firstValueString);
     
    				try {
    					if(firstValue < 0.0)	
    					{
    						throw new InvalidTemperatureException(firstValue);
    					}
    					celsiusValueField.setText("" + kelvinToCelsius(firstValue));
    					fahrenheitValueField.setText("" + kelvinToFahrenheit(firstValue));
    				}//end try
    				catch(InvalidTemperatureException ex)
    				{
    					JOptionPane.showMessageDialog(topPanel, "InvalidTemperatureException:" +
    												" the entered temperature of " + firstValue + " is below absolute zero");
    					kelvinValueField.setText("");
    					kelvinValueField.requestFocus();
    				}//end catch
    			}//end if
    		}//end actionPerformed
     
    		public double celsiusToKelvin(double temp) {
    			return temp + 273.15;
    		}//end method
     
    		public double kelvinToCelsius(double temp) {
    			return temp - 273.15;
    		}//end method
     
    		public double celsiusToFahrenheit(double temp) {
    			return 9.0/5.0 * temp + 32;
    		}//end method
     
    		public double fahrenheitToCelsius(double temp) {
    			return 5.0/9.0 * (temp - 32);
    		}//end method
     
    		public double fahrenheitToKelvin(double temp) {
    			fahrenheitToCelsius(temp);
    			return celsiusToKelvin(temp);
     
    		}//end method
     
    		public double kelvinToFahrenheit(double temp) {
    			kelvinToCelsius(temp);
    			return celsiusToFahrenheit(temp);
     
    		}//end method
     
    		public void focusGained(FocusEvent f) {
    			if(f.getSource().equals(fahrenheitValueField))
    			{
    				fahrenheitValueField.setText("");
    			}
     
    			if(f.getSource().equals(celsiusValueField))
    			{
    				celsiusValueField.setText("");
    			}
     
    			if(f.getSource().equals(kelvinValueField))
    			{
    				kelvinValueField.setText("");
    			}	
    		}//end method
     
    		public void focusLost(FocusEvent arg0) {
     
    		}//end method
    	}//end inner class+
     
    	public class InvalidTemperatureException extends Exception
    	{
    		//constructor
    		InvalidTemperatureException(double t)
    		{
    			//message to be passed up to exception class
    			super("Invalid temperature value of " + t + " was entered");
    			firstValue = t;
     
    		}//end method
    	}//end class
    }//end class

    errors are:

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Unkn own Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at TempConverter_C_C$ButtonHandler.actionPerformed(Te mpConverter_C_C.java:117)
    at javax.swing.JTextField.fireActionPerformed(Unknown Source)
    at javax.swing.JTextField.postActionEvent(Unknown Source)
    at javax.swing.JTextField$NotifyAction.actionPerforme d(Unknown Source)
    at javax.swing.SwingUtilities.notifyAction(Unknown Source)
    at javax.swing.JComponent.processKeyBinding(Unknown Source)
    at javax.swing.JComponent.processKeyBindings(Unknown Source)
    at javax.swing.JComponent.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unkn own Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEv ent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKe yEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAsse rtions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent (Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,571
    Thanks
    49
    Thanked 2,232 Times in 2,204 Posts

    Default Re: GUI temp converter program troubles

    NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Unkn own Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at TempConverter_C_C$ButtonHandler.actionPerformed(Te mpConverter_C_C.java:117)
    At line 117 in your code you are trying to convert an empty String to a double.
    The JVM does not like that and throws this exception.
    You either need to test the String you are trying to convert to make sure it is not empty
    or you need to catch the exception and do something like asking the user to enter a valid number.

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: GUI temp converter program troubles

    Okay, thanks for the reply. Would you mind providing some code as an example of how to do that? Also do you see anything wrong with the kelvin either of the kelvin conversion methods that would cause the results to be sent to incorrect textFields?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,571
    Thanks
    49
    Thanked 2,232 Times in 2,204 Posts

    Default Re: GUI temp converter program troubles

    A sample pseudo code
    String val = text.getText();  // get user input from text field
    if val is empty{
      ask user to provide data for the text field
    }else {
      convert val to double
      compute values
      display values
    }

  7. The Following User Says Thank You to Norm For This Useful Post:

    copelandtml (August 13th, 2011)

  8. #7
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: GUI temp converter program troubles

    Would that just need to be added or would other code need to be removed? It seems to be that the kelvin conversions will only work if numbers are already in the celsius and fahrenheit fields. The kelvin conversions still seem to be incorrect though.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,571
    Thanks
    49
    Thanked 2,232 Times in 2,204 Posts

    Default Re: GUI temp converter program troubles

    You would need to change your code to get the value into a variable and test it BEFORE trying to convert it to a double.

    the kelvin conversions will only work if numbers are already in the celsius and fahrenheit fields.
    If some of the code works and some doesn't. Check what the difference is between those that work and those that don't and change the broken one to work like the one that works.
    The kelvin conversions still seem to be incorrect though.
    Check the equation that you are using. Does it generate the correct results?

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,571
    Thanks
    49
    Thanked 2,232 Times in 2,204 Posts

    Default Re: GUI temp converter program troubles

    e.getSource().equals(fahrenheitValueField))
    This is one of the times when you should use the == operator instead of the equals() method. The getSource() method returns a reference to the component that sent the event. You want to compare that reference to references to each component to see if they are the one that sent the event. It seems to work here, but that is a surprise to me

  11. The Following User Says Thank You to Norm For This Useful Post:

    copelandtml (August 14th, 2011)

  12. #10
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: GUI temp converter program troubles

    Thanks for all the help! Found my main problem, still had to switch a few things around in the kelvin methods but the " if " statement on line 107 had a semicolon on the end.

    (
    if(e.getSource().equals(celsiusValueField));

  13. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,571
    Thanks
    49
    Thanked 2,232 Times in 2,204 Posts

    Default Re: GUI temp converter program troubles

    What compiler are you using? Mine gives me this warning:

    TempConverter_C_C.java:121: warning: [empty] empty statement after if
    			if(e.getSource().equals(celsiusValueField));
    			                                                          ^
    1 warning

Similar Threads

  1. Military time converter
    By ebone in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2011, 02:56 AM
  2. J2ME converter program
    By sackling in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 6th, 2010, 08:13 AM
  3. converter program, problem with action listener
    By robertson.basil in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2010, 05:44 AM
  4. phonetic to cyrillic converter
    By Brt93yoda in forum Java Theory & Questions
    Replies: 1
    Last Post: September 5th, 2010, 02:26 PM
  5. Piglatin Converter
    By jross21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 12:09 PM