Results 1 to 7 of 7
  1. #1
    ExX
    ExX is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Exception handling

    Hey guys just working on a java homework again, it prompts the user twice, asking the user to enter a part number, and quantity, and both entry have to be int data type. I created a class that holds an array of error messages and called it DataMessages and it has a getMessage method. Code below:
    Java Code:
    public class DataMessages
    {
    	String[] msgArray = new String[6];
    	public DataMessages()
    	{
    		msgArray[0] = "The part number is not numeric.";
    		msgArray[1] = "The quantity is not numeric.";
    		msgArray[2] = "The part number is too low(less than 0).";
    		msgArray[3] = "The part number is too high (more than 999).";
    		msgArray[4] = "The quantity ordered is too low (less than 1).";
    		msgArray[5] = "The quantity ordered is too high (more than 5,000).";
    	}
    	public String getMessage(int num)
    	{
    		return msgArray[num];
    	}
    
    }
    There is also a class I created to extend the exception class as shown below.
    Java Code:
    public class DataException extends Exception
    {
    	DataMessages msg = new DataMessages();
    	
    	public DataException(int num)
    	{
    		//Display message when the exception is caught
    		super("Error Code - " + num);
    		System.out.println(msg.getMessage(num));
    	}
    	
    }
    So the error message will be displayed according to what the user enters. But here is my problem, error message 0 and 1 won't be able to display because java immediately does the inputmismatchexception and display only one of the error message regardless of which prompt they enter it for when the user enters a string. How can I make it so that when the user enter a string for the first prompt it will display error message 0 and display error message 1 for the 2nd prompt? Here is the actual class that test all of this:
    Java Code:
    // Xianyi Ye
    // 3/11/2012
    // Homework page 575 #10
    import java.util.*;
    public class PartAndQuantityEntry
    {
    	public static void main(String[] args)throws DataException
    	{
    		Scanner scan = new Scanner(System.in);
    		int numP;
    		int numQ;
    		int terminal = 0;
    		int returnValue = terminal + 1;
    		
    		//Terminates the program if the return value isn't 0
    		while(returnValue != terminal)
    		{		
    			try
    			{
    				System.out.println("Enter 01 for both entry to quit\nPlease enter a part number: ");
    				numP = scan.nextInt();
    				System.out.println("Please enter quantity: ");
    				numQ = scan.nextInt();
    				returnValue = check(numP, numQ);
    			}
    			catch(DataException error)
    			{
    				System.out.println(error.getMessage());				
    			}
    			catch(InputMismatchException error)
    			{
    				int num = 0;
    				throw (new DataException(num));			
    			}			
    		}
    	}
    	public static int check(int numP, int numQ) throws DataException
    	{
    		final int MAXP = 999;
    		final int MINP = 0;
    		final int MAXQ = 5000;
    		final int MINQ = 1;
    		final int QUIT = 01;
    		int returnVal = 1;
    
    		if(numP < MINP)
    		{
    			numP = 2;
    			throw (new DataException(numP));
    		}
    		if(numP > MAXP)
    		{
    			numP = 3;	
    			throw (new DataException(numP));
    		}
    		if(numQ < MINQ)
    		{
    			numQ = 4;
    			throw (new DataException(numQ));
    		}
    		if(numQ > MAXQ)
    		{
    			numQ = 5;
    			throw (new DataException(numQ));
    		}
    		if(numP == QUIT && numQ ==QUIT)
    			returnVal = 0;
    		else
    			System.out.println("Valid entry");
    		return returnVal;
    	}
    }
    Last edited by ExX; 03-12-2012 at 01:47 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Exception handling

    Does that compile?

  3. #3
    ExX
    ExX is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Exception handling

    Yes all compiles and able to run it's just that when I try to get error message 0 and 1 I can't get it.

    Like I try to enter a string for input for the first prompt but it only shows message 0 because i coded it like this:
    Java Code:
    			catch(InputMismatchException error)
    			{
    				int num = 0;
    				throw (new DataException(num));			
    			}
    Last edited by ExX; 03-12-2012 at 01:49 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Exception handling

    When I first looked at it the code had an expression like "check(numP)" which was worth thinking about (and to which the compiler would have alerted you). Now the check() method is not used at all - leaving the catch(DataException) block doing nothing.

    Personally I think the errant check(numP) might have pointed the way forward: since you are checking two values independently have two sets of try/catch blocks, and two different check methods each taking a single argument (the thing to be checked).

  5. #5
    ExX
    ExX is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Exception handling

    thank you, I figured it out when you pointed out check(numP), which I previously minstakenly put but now I just made another check method to determine how to call the right error message thank you very much.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Exception handling

    You're welcome.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Exception handling

    You've made something of a classic exception mistake here (and it's one that exists in several applications I've seen in use in businesses, so don't feel bad).
    Those are all different exceptions (with the possible exception of the first two, though personally I'd still have them separate).
    You might have them as part of a hierarchy, but they should be different classes.
    This would solve most of your problem, to be honest.

    The use of ids to differentiate exceptions is a mistake.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Exception handling
    By Moedig in forum New To Java
    Replies: 2
    Last Post: 11-10-2011, 10:43 PM
  2. Exception handling
    By herat in forum New To Java
    Replies: 1
    Last Post: 06-21-2011, 10:13 AM
  3. Exception Handling
    By link6790 in forum New To Java
    Replies: 16
    Last Post: 05-19-2011, 06:57 PM
  4. Exception Handling
    By eLancaster in forum New To Java
    Replies: 4
    Last Post: 02-20-2011, 12:00 AM
  5. Exception Handling help
    By MZA in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 09:23 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
  •