I'm trying to create a simple program that will check for any given number (an ID) provided by the user and check this ID with an array of numbers inside my program. If the correct ID is given, it will display "Valid ID" if the ID provided by the user is not found, it will display "Invalid ID"

I'm stuck in the part where to use the exceptions. I'm quite lost there. What I can do with what I know is verify the ID provided by the user and check if it's on my array or not. Then I display the message if it's found or not. The problem is, it's supposed to display the exception if the ID is invalid.

My code is as follows:

class InvalidIDException extends Exception
{
	InvalidIDException()
	{
		super("Invalid ID");
	}
}

class CheckID{
	static int TheArray[] = {10, 11, 12, 13, 14, 15};
	static int count = 0;
	
	public static void CheckNumber (int number){
		for (int x = 0; x < 6; x++){
			if (number == TheArray[x]){
				count = 1;
				break;
			}
		}
		if (count == 1)
			System.out.println("Valid ID");
		else
			System.out.println("Invalid ID");
	}
	
	public static void main(String[] args){
		int number = Integer.parseInt(args[0]);
		CheckNumber(number);
	}
}

Could you guys help me figure out the rest?

if (count == 1) {
            System.out.println("Valid ID");
        } else {
            System.out.println("Invalid ID");
            throw new InvalidIDException();
        }
    }

    public static void main(String[] args) throws InvalidIDException {
        int number = Integer.parseInt(args[0]);
        CheckNumber(number);
    }

^ That code looks wrong. Who modifies main to throw an Exception? Also, since his Exception is printing "Invalid ID", your code would print the same thing twice. And most importantly, when you declare a method as 'throws Exception' in the method header, the method that calls it has to handle the exception by using try/catch. So, for example, Mr.Diaz, see my code below

public static void checkNumber (int number) throws InvalidIDException{
-loop through the array, find out if the ID is valid
if ID is not valid, throw new InvalidIDException
else do nothing, since the ID is valid
}

main method{
   try{
    checkNumber(number);
    System.out.println("Valid ID!");
   } catch(InvalidIDException e){
      System.out.println(e.getMessage());
   }
}

Mr Diaz, please note that this line of code: System.out.println("Valid ID!"); will never be reached if checkNumber throws an Exception. That is because if checkNumber throws an exception, then the code in our main method will 'go' to the catch portion, and it will never execute anything after checkNumber(number). Otherwise, if there is no exception, it will do the print statement and it will never enter the catch portion.

commented: Who modifies main...? - No one, thank you for assertive post. quuba +1

Thanks a lot BestJewSinceJC! That helps a lot. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.