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!
Code :
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
Re: GUI temp converter program troubles
Quote:
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.
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:
Code :
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)
Re: GUI temp converter program troubles
Quote:
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.
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?
Re: GUI temp converter program troubles
A sample pseudo code
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
}
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.
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.
Quote:
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.
Quote:
The kelvin conversions still seem to be incorrect though.
Check the equation that you are using. Does it generate the correct results?
Re: GUI temp converter program troubles
Code :
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
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.
(
Code :
if(e.getSource().equals(celsiusValueField));
Re: GUI temp converter program troubles
What compiler are you using? Mine gives me this warning:
Code :
TempConverter_C_C.java:121: warning: [empty] empty statement after if
if(e.getSource().equals(celsiusValueField));
^
1 warning