Results 1 to 7 of 7
  1. #1
    litojs is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default [SOLVED] Some questions about exception

    Java Code:
    import javax.swing.JOptionPane;
    public class UseStaff
    {
    	public static void main(String[] args)
    	{
    		int choice = 0;
    	
    	try
    	{
    		do
    		{
    			choice = Integer.parseInt(JOptionPane.showInputDialog(null,"This is a Program to calculate your salary.\n\nPlease choose:\n1 for full time staff\n2 for part time staff\n3 to exit the program"));	
    			switch(choice)
    			{
    			case 1:
    			JOptionPane.showMessageDialog(null,"You choose full time staff calculation.");
    			break;
    			case 2:
    			JOptionPane.showMessageDialog(null,"You choose part time staff calculation.");
    			break;
    			case 3:
    			JOptionPane.showMessageDialog(null,"You are exiting the program.");
    			System.exit(0);
    			default:
    			JOptionPane.showMessageDialog(null,"Invalid option!");
    			}	
    		}while(choice < 1 || choice > 3);
    	}
    	
    
    	catch(NumberFormatException exception)
    	{
    		JOptionPane.showMessageDialog(null,"The application only accepts digit");
    	}
    
    	}
    }
    here comes the question:
    can i repeat the loop after it caught an exception?
    if yes how should i arrange my code?

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    re-entering loop after exception tends to get stuck loops and hung machines. We can do if exception, value == default value and continue.

    Be careful with this, what I do is if( value not within range )value = defaultValue; which allows exception (s) to be used as exceptions.

    To do otherwise is not only dicey and problematic but as well conceals problems that could show up at the wrong time.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    litojs is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    Java Code:
    import javax.swing.JOptionPane;
    public class UseStaff
    {
    	public static void main(String[] args)
    	{
    		int choice = 0;
    	
    		do
    		{
    		try
    		{
    			choice = Integer.parseInt(JOptionPane.showInputDialog(null,"This is a Program to calculate your salary.\n\nPlease choose:\n1 for full time staff\n2 for part time staff\n3 to exit the program"));	
    		}
    		catch(NumberFormatException exception)
    		{
    			JOptionPane.showMessageDialog(null,"The application only accepts digit");
    		}	
    			switch(choice)
    			{
    			case 1:
    			JOptionPane.showMessageDialog(null,"You choose full time staff calculation.");
    			break;
    			case 2:
    			JOptionPane.showMessageDialog(null,"You choose part time staff calculation.");
    			break;
    			case 3:
    			JOptionPane.showMessageDialog(null,"You are exiting the program.");
    			System.exit(0);
    			default:
    			JOptionPane.showMessageDialog(null,"Invalid option!");
    			}	
    		}while(choice < 1 || choice > 3);
    	}	
    }
    while im waiting for help, i tried changing the code myself and this worked too.
    will i encounter those problem u mention later on?

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Yes, sooner or later. Bad part is if it happens in front of somebody else or damages data that cannot be recovered.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  5. #5
    litojs is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    can you give me an example of that IF statement u mention?

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Java Code:
    if(args[0] != null){}else{}
    Java Code:
    if(args[0].length > 0){}else{}
    Java Code:
    if(choice != null){}else{}
    btw, you are doing Integer.parseInt() which returns an int but I think JOption pane returns an int so Integer.parseInt() would not be correct here.
    Last edited by Nicholas Jordan; 11-16-2008 at 04:34 PM. Reason: code tags
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  7. #7
    litojs is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    thanks for the example..now i know abit what you are talking about.
    btw, you are doing Integer.parseInt() which returns an int but I think JOption pane returns an int so Integer.parseInt() would not be correct here.
    JOptionPane always return a String. this im certain. =)
    Last edited by litojs; 11-16-2008 at 05:54 PM.

Similar Threads

  1. questions for 1yr exp
    By rahaman.athiq in forum Java Servlet
    Replies: 2
    Last Post: 11-26-2008, 01:13 AM
  2. I have Questions -_-
    By ChazZeromus in forum New To Java
    Replies: 5
    Last Post: 09-13-2008, 08:08 PM
  3. Replies: 1
    Last Post: 03-08-2008, 06:41 PM
  4. questions
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 11-27-2007, 11:18 PM
  5. 3 Questions
    By hiranya in forum AWT / Swing
    Replies: 4
    Last Post: 11-14-2007, 04:57 AM

Posting Permissions

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