Results 1 to 10 of 10
  1. #1
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Question Error with my Try Catch

    Hello to all

    I am not receiving any compiling errors with my code. But my code is not printing out the error message in the JOptionPane window and I can't figure out why. Here is my code. My objective is.


    If anything other than a number is entered including no input at all, then the warning joption pane window should appear with the message I have below in my code.


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    
    
    public class StaffingProgram extends JFrame
    {
    	private JLabel resultAL, resultBL, resultCL, outputAL, outputBL, outputCL;
    	private JTextField resultATF, resultBTF, resultCTF, outputATF, outputBTF, outputCTF;
    	private JButton calculateB, exitB;
    	private CalculateButtonHandler cbHandler;
    	private ExitButtonHandler ebHandler;
    	
    
    	private static final int WIDTH = 900;
    	private static final int HEIGHT = 600;
    
    	public StaffingProgram()
    	{
    	
    		
    		resultAL = new JLabel("Please enter the number of employees presently on duty at location A: ", 
    			SwingConstants.RIGHT);
    		resultBL = new JLabel("Please enter the number of employees presently on duty at location B: ",
    			SwingConstants.RIGHT);
    		resultCL = new JLabel("Please enter the number of employees presently on duty at location C: ",
    			SwingConstants.RIGHT);
    		outputAL = new JLabel("Number of employees under or over for location A: ",
    			SwingConstants.RIGHT);
    		outputBL = new JLabel("Number of employees under or over for location B: ",
    			SwingConstants.RIGHT);
    		outputCL = new JLabel("Number of employees under or over for location C: ",
    			SwingConstants.RIGHT);
    		
    		
    		
    		
    		
    		
    		resultATF = new JTextField(10);
    		resultBTF = new JTextField(10);
    		resultCTF = new JTextField(10);
    		outputATF = new JTextField(10);
    		outputBTF = new JTextField(10);
    		outputCTF = new JTextField(10);
    		
    		
    		
    
    		calculateB = new JButton("Calculate");
    		cbHandler = new CalculateButtonHandler();
    		calculateB.addActionListener(cbHandler);
    
    		exitB = new JButton("Exit");
    		ebHandler = new ExitButtonHandler();
    		exitB.addActionListener(ebHandler);
    		
    
    		setTitle("Staffing Level Calculations");
    
    		Container pane = getContentPane();
    
    		pane.setLayout(new GridLayout(7, 2));
    
    		
    		pane.add(resultAL);
    		pane.add(resultATF);
    		pane.add(resultBL);
    		pane.add(resultBTF);
    		pane.add(resultCL);
    		pane.add(resultCTF);
    		pane.add(outputAL);
    		pane.add(outputATF);
    		pane.add(outputBL);
    		pane.add(outputBTF);
    		pane.add(outputCL);
    		pane.add(outputCTF);
    		pane.add(calculateB);
    		pane.add(exitB);
    
    		setSize(WIDTH, HEIGHT);
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
    
    	private class CalculateButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			double resulta, resultb, resultc;
    			
    			
    			resulta = Double.parseDouble(resultATF.getText());
    			resultb = Double.parseDouble(resultBTF.getText());
    			resultc = Double.parseDouble(resultCTF.getText());
    			
    	try
    	{
    			if (resulta > 5){
    			resulta = resulta - 5;
    			outputATF.setText("You are overstaffed by " + resulta);
    			}
    			else if (resulta < 5){
    			resulta = 5 - resulta;
    			outputATF.setText("You are understaffed by " + resulta);
    			}
    			else if (resulta == 5){
    			outputATF.setText("Your staffing is sufficient");
    			}
    			
    			
    			if (resultb > 8){
    			resultb = resultb - 8;
    			outputBTF.setText("You are overstaffed by " + resultb);
    			}
    			else if (resultb < 8){
    			resultb = 8 - resultb;
    			outputBTF.setText("You are understaffed by " + resultb);
    			}
    			else if (resultb == 8){
    			outputBTF.setText("Your staffing is sufficient");
    			}
    			
    						
    
    			if (resultc > 10){
    			resultc = resultc - 10;
    			outputCTF.setText("You are overstaffed by " + resultc);
    			}
    			else if (resultc < 10){
    			resultc = 10 - resultc;
    			outputCTF.setText("You are understaffed by " + resultc);
    			}
    			else if (resultc == 10){
    			outputCTF.setText("Your staffing is sufficient");
    			}
    
    			else
    			{
    			throw new TheException(); //calls
                		}
    	}
    			catch (TheException ev)
            		{
    			JOptionPane.showMessageDialog(null, "" +ev + " You must enter a number", "Invalid Input", 		
    
    			JOptionPane.INFORMATION_MESSAGE);
           			}
    
    		}
    	}
    
    
    class TheException extends Exception
    {
    	public String toString()
    	{
    		return "Illegal";
    	}
    }
    
    
    
    	private class ExitButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.exit(0);
    		}
    	}
    	
    	public static void main(String[] args) throws IOException
    	{
    		StaffingProgram rectObject = new StaffingProgram();
    	}
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Because catch the exception in wrong place.

    Java Code:
    resulta = Double.parseDouble(resultATF.getText());
    What can be here, if your input is not a number you get NumberFormatException. Your try-catch block not caught such thing.

  3. #3
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    ev in TheException is just a user-defined object....
    It doesn't automatically convert to String....

    Unless, you call the toString() method....
    freedom exists in the world of ideas

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    He needs to be catched that NumberFormatException.....

    That TheException may be catched if ( i don't know what he is trying to do with that ).... :)
    freedom exists in the world of ideas

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, that's why I worried too sukato. See his try block. Just comparing integer value, and if he fails throw the exception. So, what he going to do.

    Basically he's dealing with numbers, and the most perfect thing is to catch the number format exception. That mean he has to check that given data is valid or not.

  7. #7
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    I think its funny that the part that needs to be in a try catch isn't. You should put the parsing into a number format Exception. Then in your else statement display the info the error info.
    My IP address is 127.0.0.1

  8. #8
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Basically he's dealing with numbers, and the most perfect thing is to catch the number format exception. That mean he has to check that given data is valid or not.
    Yap.... absolutely... :)
    freedom exists in the world of ideas

  9. #9
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Default Error with my Try Catch

    Eranga
    Zosden
    Sukatoa

    Thank you for your input. I am not very familiar with what you are talking about so I did a little reading online about the number format exception. I changed my coding up a bit and put the parsing within the try statement and changed it.
    Double ---> Integer and placed within try catch statement
    Deleted ---> NumberFormatException extends Exception section


    This worked out great. Thank you all for your support and help on this. All of us have our own way of learning and mine unfortunately requires a lot of help and pointing in the right direction. I am not that good at reading between the lines. :) I needed to hear the number format exception and the statement about placing the parsing into the try statement in order to solve this.

    Thanks again. here is what I did.

    Java Code:
    try
    	{
    			resulta = Integer.parseInt(resultATF.getText());
    			resultb = Integer.parseInt(resultBTF.getText());
    			resultc = Integer.parseInt(resultCTF.getText());
    			
    			if (resulta > 5){
    			resulta = resulta - 5;
    			outputATF.setText("You are overstaffed by " + resulta);
    			}
    			else if (resulta < 5){
    			resulta = 5 - resulta;
    			outputATF.setText("You are understaffed by " + resulta);
    			}
    			else if (resulta == 5){
    			outputATF.setText("Your staffing is sufficient");
    			}
    			
    			
    			if (resultb > 8){
    			resultb = resultb - 8;
    			outputBTF.setText("You are overstaffed by " + resultb);
    			}
    			else if (resultb < 8){
    			resultb = 8 - resultb;
    			outputBTF.setText("You are understaffed by " + resultb);
    			}
    			else if (resultb == 8){
    			outputBTF.setText("Your staffing is sufficient");
    			}
    			
    						
    
    			if (resultc > 10){
    			resultc = resultc - 10;
    			outputCTF.setText("You are overstaffed by " + resultc);
    			}
    			else if (resultc < 10){
    			resultc = 10 - resultc;
    			outputCTF.setText("You are understaffed by " + resultc);
    			}
    			else if (resultc == 10){
    			outputCTF.setText("Your staffing is sufficient");
    			}
    
    			else
    			{
    			throw new NumberFormatException(); //calls
                		}
    	}
    			catch (NumberFormatException ev)
            		{
    			JOptionPane.showMessageDialog(null, "" +ev + " You must enter a number", "Invalid Input", 		
    
    			JOptionPane.INFORMATION_MESSAGE);
           			}

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Again, your exception doesn't convert into string. Try something like this.

    Java Code:
    JOptionPane.showMessageDialog(null, "" + ev.getMessage() + " You must enter a number", "Invalid Input", JOptionPane.INFORMATION_MESSAGE);

Similar Threads

  1. try catch!?
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 01-28-2008, 08:51 PM
  2. Try Catch issue
    By curtis_fraser in forum Advanced Java
    Replies: 2
    Last Post: 12-14-2007, 12:04 AM
  3. Try Catch
    By Renegade85 in forum New To Java
    Replies: 4
    Last Post: 12-03-2007, 05:10 PM
  4. when to use try...catch
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 09:52 PM
  5. Use try and catch
    By zoe in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 08:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •