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

Thread: Experiencing a run time error, don't know what the problem is

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Experiencing a run time error, don't know what the problem is

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Experiencing a run time error, don't know what the problem is

    NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:470)
    at java.lang.Integer.parseInt(Integer.java:499)
    at Season.keyPressed(Season.java:49)
    Look at line 49 in Season.java where the call to parseInt is made and see why the arg for the method is ""

    For example:
    Integer.parseInt(inputTemp.getText());

    Do this in two steps. Get the contents of the text field and validate it before trying to use it.

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

    scooty199 (October 3rd, 2010)

  4. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Experiencing a run time error, don't know what the problem is

    I did find my error. I realized in my keyPressed method I should have out the parseInt under the if condition.

    Thanks for the help!

Similar Threads

  1. Using time
    By ellias2007 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 4th, 2010, 08:48 AM
  2. Problem in rotating and moving image at the same time
    By SlimShady in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 17th, 2010, 02:33 PM
  3. Struts Application, Complies perfectly but error at run time
    By Prarthana in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 18th, 2010, 01:49 AM
  4. display time
    By kalees in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 1st, 2010, 07:40 AM
  5. SQL Time Calculation
    By r12ki in forum JDBC & Databases
    Replies: 3
    Last Post: July 31st, 2009, 03:54 AM