Ok, so in my GUI application, it takes a string and converts it to a number, but there's a problem.
It generates a run time error
Here's the cod]import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Season extends JFrame implements ActionListener, KeyListener { private JTextField inputTemp, dispSeason; private JButton determ; public Season() { super("Season.java"); JLabel prompt = new JLabel("Enter a temperature"); inputTemp = new JTextField(1); inputTemp.addKeyListener(this); JLabel result = new JLabel("The season probably is:"); dispSeason = new JTextField(1); dispSeason.setEditable(false); determ = new JButton("Determine"); determ.addActionListener(this); Container c = getContentPane(); c.setBackground(Color.white); JPanel p = new JPanel(); p.setLayout(new GridLayout(4,2)); p.add(prompt); p.add(inputTemp); p.add(result); p.add(dispSeason); p.add(determ); c.add(p, BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) { int temp = Integer.parseInt(inputTemp.getText()); checkSeason(temp); } public void keyPressed(KeyEvent e1) { int key = e1.getKeyCode(); int temp = Integer.parseInt(inputTemp.getText()); if(key == KeyEvent.VK_ENTER) checkSeason(temp); } public void keyTyped(KeyEvent e){} public void keyReleased(KeyEvent e){} public void checkSeason(int temper) { if(temper > 110 || temper < -5) dispSeason.setText("Invalid Temperature"); else if(temper >= 90) dispSeason.setText("Summer"); else if(temper >= 70 && temper <90 ) dispSeason.setText("Spring"); else if(temper >= 50 && temper <70) dispSeason.setText("Fall"); else if(temper < 50) dispSeason.setText("Winter"); } public static void main(String[] args) { Season sea = new Season(); sea.setBounds(300,300,400,200); sea.setDefaultCloseOperation(EXIT_ON_CLOSE); sea.setVisible(true); } }
Here's what error says:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:470) at java.lang.Integer.parseInt(Integer.java:499) at Season.keyPressed(Season.java:49) at java.awt.Component.processKeyEvent(Component.java:6225) at javax.swing.JComponent.processKeyEvent(JComponent.java:2801) at java.awt.Component.processEvent(Component.java:6044) at java.awt.Container.processEvent(Container.java:2041) at java.awt.Component.dispatchEventImpl(Component.java:4630) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1850) at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:712) at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:990) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:855) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:676) at java.awt.Component.dispatchEventImpl(Component.java:4502) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Window.dispatchEventImpl(Window.java:2478) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Thank you if you can help.