Hi, can someone help we please?
I need to write a temperature-conversion application that converts from fahrenheit to celsius. The temperature should be entered from the keyboard(via a jtextfield). And a label should be used to display the converted temperature.
This is mine code:
import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.JTextField; import javax.swing.JLabel; import javax.swing.JFrame; import javax.swing.JPanel; public class Conversion extends JFrame{ private final JTextField fField; private final JLabel cLabel; private final JLabel label; private final JLabel fLabel; private final JPanel panel; private final JPanel fPanel; private final JPanel cPanel; public Conversion(){ super("Temperature Conversion"); setLayout(new FlowLayout()); panel = new JPanel(new GridLayout(2,1)); fPanel = new JPanel(new GridLayout(1,2)); cPanel = new JPanel(new GridLayout(1,2)); fLabel = new JLabel("Enter fahrenheit: "); fField = new JTextField(5); fPanel.add(fLabel); fPanel.add(fField); int f = Integer.parseInt(fField.getText()); int c = 5/9 * (f - 32); label = new JLabel(); label.setText(Integer.toString(c)); cLabel = new JLabel("Celsius: "); cPanel.add(cLabel); cPanel.add(label); panel.add(fPanel); panel.add(cPanel); add(panel); } }
This is the error i'm getting:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at Conversion.<init>(Conversion.java:32)
at ConversionTest.main(ConversionTest.java:5)
C:\Users\Maneesha\AppData\Local\NetBeans\Cache\10. 0\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\Maneesha\AppData\Local\NetBeans\Cache\10. 0\executor-snippets\run.xml:68: Java returned: 1