Results 1 to 9 of 9
  1. #1
    Scopic is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default First try at exceptions, running into some issues!

    Okay so, I tried my hand at a couple exceptions before this and they turned out fine, but the way this function was formed is making it a little difficult for me to formulate the correct logic for the exception handling. Let's get to it:

    Here's the function that throws the exception:

    Java Code:
    public void removeObject(T anObject)
                   throws CannotRemoveException
           {
             String paramClass = anObject.getClass().getName();
             String currClass;
             
           // Logic ...
             for (T currT: theList)
             {
                currClass = currT.getClass().getName();
    
                if (currClass.equals(paramClass))
                {
                   if (currT.equals(anObject))
                   {
                      theList.remove(anObject);
                      System.out.println("Member found and has been removed from "
                              + "the set.");
                   }
                }
             }
             throw new CannotRemoveException(anObject);
           }
    What this function does is check for an object match for a specific class inside of the array, theList, and removes it.
    The exception, CannotRemoveException, is to be thrown when the object that the user will input is NOT FOUND in the array (does not match any members of the array). It then displays appropriate feedback and returns the user to the main menu.

    The function call in main is where I'm having trouble:

    Here's the function call BEFORE being modified for the exception:

    Java Code:
    //remove subscriber from Element Set array
                    System.out.println("Enter Subscriber's name: ");
                    search = keyboard.nextLine().toUpperCase();
                    
                     for(int i = 0; i < anElementSet.size(); i++)
                    {
                        anElement = anElementSet.getCurrent();
    
                        if(anElement instanceof Subscriber)
                        { 
                            aSub =(Subscriber)anElement;
                        
                            if((aSub.getName()).equals(search))
                            {
                                anElementSet.removeObject(aSub);
                            }
    
                            else
                            {                            
                                System.out.println("Member not found, "
                                        + "removal unsuccessful.");
                            }
                        }
                    }
    Now let me describe where I'm having trouble. I can't quite figure out where I would begin the try/catch functions in main where the removeObject function is called.

    As you may have noticed, the if and else statements surrounding the function call are already checking to see if what the user entered matches anything in the array and handles it if it is not.

    What I am basically asking is, how would I go about changing this:


    Java Code:
                  if((aSub.getName()).equals(search))
                            {
                                anElementSet.removeObject(aSub);
                            }
    
                            else
                            {                            
                                System.out.println("Member not found, "
                                        + "removal unsuccessful.");
                            }
    Into something like this:

    Java Code:
                 
                                try
                                {
                                anElementSet.removeObject(aSub);
                                }
                                catch(CannotRemoveException e)
                                {
                                    System.out.println("Member not found, "
                                        + "removal unsuccessful.");
                                }
    Thank you for any help!
    Last edited by Scopic; 12-03-2011 at 07:49 AM.

  2. #2
    Rewry is offline Member
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: First try at exceptions, running into some issues!

    Hi,

    In your first method, the exception will always be thrown even when the object is found in the list or is not. There is no branching sequence which will not throw the exception, meaning that for the method to finish, it must throw the exception. What I would do is implement a simple integer counter, which will increment every time you find and remove an object from the list. Once the loop is over, you write a simple if statement checking whether the counter is equal to 0. If it is, meaning that the object didnt find a match, you throw the exception. Otherwise, you do nothing and the method ends

  3. #3
    Scopic is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: First try at exceptions, running into some issues!

    Oh I see what you mean.

    Something like this?

    Java Code:
    for (T currT: theList)
             {
                currClass = currT.getClass().getName();
    
                if (currClass.equals(paramClass))
                {
                   if (currT.equals(anObject))
                   {
                      theList.remove(anObject);
                      System.out.println("Member found and has been removed from "
                              + "the set.");
                      removalCount++;
                   }              
                }
             }
             
             if (removalCount == 0)
             {
                 throw new CannotRemoveException();
             }
           }
    Also, I am still trying to fix the logic for the function call in main. This is how it's setup now and I know it's wrong:

    Java Code:
     //remove subscriber from Element Set array
                    System.out.println("Enter Subscriber's name: ");
                    search = keyboard.nextLine().toUpperCase();
                    
                     for(int i = 0; i < anElementSet.size(); i++)
                    {
                        anElement = anElementSet.getCurrent();
    
                        if(anElement instanceof Subscriber)
                        { 
                            aSub =(Subscriber)anElement;
                        
                            if((aSub.getName()).equals(search))
                            {
                                try
                                {
                                anElementSet.removeObject(aSub);
                                }
                                catch(CannotRemoveException e)
                                {
                                    System.out.println("Member not found, "
                                        + "removal unsuccessful.");
                                }
                            }
    These lines:

    Java Code:
    if(anElement instanceof Subscriber)
                        { 
                            aSub =(Subscriber)anElement;
                        
                            if((aSub.getName()).equals(search))
                            {
    are already doing the check to make sure that what the user entered is correct. I am trying to figure out a way for the logic to be like this:

    1.) User enters name of object
    2.) Program first checks that the object belongs to the correct class
    3.) Program then TRIES to remove the object
    4.) In doing so, the program checks to see whether or not the object with that name is in the set. If it is NOT in the set, then it throws the CannotRemoveException and loops back into the main menu.

    As of right now I have it checking to make sure that everything matches before it even tries the removal; this results in the removeObject method only executing if the user inputs the name of an object in the set.

  4. #4
    Scopic is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: First try at exceptions, running into some issues!

    So I modified the code a little bit, but I'm still having some issues:

    Call in main:

    Java Code:
    //remove subscriber from Element Set array
                    System.out.println("Enter Subscriber's name: ");
                    search = keyboard.nextLine().toUpperCase();
                    
                    if(anElementSet.isEmpty())
                    {
                        System.out.println("\nThe set is empty, please add members"
                                + " first.");
                    }
    
                         for(int i = 0; i < anElementSet.size(); i++)
                        {
                            anElement = anElementSet.getCurrent();
    
                            if(anElement instanceof Subscriber)
                            { 
                                aSub =(Subscriber)anElement; 
    
                                try
                                {
                                    if((aSub.getName()).equals(search))
                                    {
                                        anElementSet.removeObject(aSub);       
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                catch(CannotRemoveException e)
                                {
                                      System.out.println("Member not found, "
                                            + "removal unsuccessful.");
                                }
    
                            }
                        }
    removeObject function:

    Java Code:
    public void removeObject(T anObject)
                   throws CannotRemoveException
           {
             String paramClass = anObject.getClass().getName();
             String currClass;
             int removalCount = 0;
             
           // Logic ...
             for (T currT: theList)
             {
                currClass = currT.getClass().getName();
    
                if (currClass.equals(paramClass))
                {
                   if (currT.equals(anObject))
                   {
                      theList.remove(anObject);
                      System.out.println("Member found and has been removed from "
                              + "the set.");
                      removalCount++;
                   }              
                }
             }
             
             if (removalCount == 0)
             {
                 throw new CannotRemoveException();
             }
    Here's what happens when I run the program and attempt to remove a non-existent object:

    -----------------------------
    Here are your Choices:
    Enter 1 to add a Subscriber
    Enter 2 to add an Application
    Enter 3 to remove a Subscriber
    Enter 4 to remove an Application
    Enter 5 to display all Subscribers
    Enter 6 to display all Applications
    Enter 7 to Quit
    -----------------------------

    1
    Subscriber's name:
    b
    Subscriber's url:
    b
    Subscriber's ad clicks:
    123

    Member successfully added to set.

    -----------------------------
    Here are your Choices:
    Enter 1 to add a Subscriber
    Enter 2 to add an Application
    Enter 3 to remove a Subscriber
    Enter 4 to remove an Application
    Enter 5 to display all Subscribers
    Enter 6 to display all Applications
    Enter 7 to Quit
    -----------------------------

    3
    Enter Subscriber's name:
    f

    -----------------------------
    Here are your Choices:
    Enter 1 to add a Subscriber
    Enter 2 to add an Application
    Enter 3 to remove a Subscriber
    Enter 4 to remove an Application
    Enter 5 to display all Subscribers
    Enter 6 to display all Applications
    Enter 7 to Quit
    -----------------------------
    Nothing happens, it stops right at this line:

    Java Code:
    anElementSet.removeObject(aSub);
    Here's what happens when I try removing an object that does exist in the set:

    -----------------------------
    Here are your Choices:
    Enter 1 to add a Subscriber
    Enter 2 to add an Application
    Enter 3 to remove a Subscriber
    Enter 4 to remove an Application
    Enter 5 to display all Subscribers
    Enter 6 to display all Applications
    Enter 7 to Quit
    -----------------------------

    1
    Subscriber's name:
    b
    Subscriber's url:
    b
    Subscriber's ad clicks:
    123

    Member successfully added to set.

    -----------------------------
    Here are your Choices:
    Enter 1 to add a Subscriber
    Enter 2 to add an Application
    Enter 3 to remove a Subscriber
    Enter 4 to remove an Application
    Enter 5 to display all Subscribers
    Enter 6 to display all Applications
    Enter 7 to Quit
    -----------------------------

    3
    Enter Subscriber's name:
    b
    Member found and has been removed from the set.
    Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Arr ayList.java:819)
    at java.util.ArrayList$Itr.next(ArrayList.java:791)
    at assignment.pkg3.Set.removeObject(Set.java:199)
    at assignment.pkg3.Assignment3.main(Assignment3.java: 156)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 6 seconds)
    I am at a loss here, I have never seen that error before. I looked it up and it seems to have something to do with my for loop iterations, but I'm not sure how to fix it.

  5. #5
    Rewry is offline Member
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: First try at exceptions, running into some issues!

    Ok I think I understand what you are trying to do... however before I propose a design, just want to clarify that u r trying to use generics by introducing T right?

  6. #6
    Scopic is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: First try at exceptions, running into some issues!

    Yes, T is a generic Set class. It was from my previous assignment, so I think I am required to use it.

  7. #7
    Rewry is offline Member
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: First try at exceptions, running into some issues!

    Note when you call the method anObject.getClass().getName(), you are not getting the subscriber's name. In other words, you are not getting the letter 'b' like as you wish in your example.

    Therefore anObject.getClass().getName() isnt like aSub.getName()

    What anObject.getClass().getName() is doing is getting the name of the Class (along with the packages), not the name of the subscriber!!!!

  8. #8
    Scopic is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: First try at exceptions, running into some issues!

    Ah, I see now. Hmmm, is there anyway to get the individual objects name outside of the class it was defined in? Not sure how to go about it any other way.

  9. #9
    Rewry is offline Member
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: First try at exceptions, running into some issues!

    Do you require for that method to be generic? Can't you just create an abstract class and name it for example RemovableObject. The abstract class should also contain an abstract method which returns a string identifying the object (I will name it getIdentifier). Note that both the subscribers class and the application class must override this method. In the case of the subscribers class, when that method is called, it will return the name of the subscriber.

    After all the classes are set up, and overriding is set where needed, you should create an ArrayList of type Subscriber (meaning ArrayList<Subscriber>). You then need to create a method to remove the object out of the list (the name of the method would be for example "removeRemovableObject"). This method should take as parameters an ArrayList of type RemovableObject (ArrayList<RemovableObject>) and a string which is used to identify the object to be removed (in this case the string would be a name of the subscriber you wish to remove).

    Inside the method, you should loop through the objects found in the ArrayList and for each iteration, you should call the getIdentifier and compare it with the string passed by the parameters. If they are equal, remove the object, and break out of the method. If no object was found, you should call your exception (I believe you named it CannotRemoveException).

    The advantage of this method, mainly due to the power of polymorphism, is that since the application class and the subscribers class are subclasses of RemovableObject, this method can be used both when removing objects of instance subscriber and objects of instance application.
    Last edited by Rewry; 12-05-2011 at 09:15 PM.

Similar Threads

  1. Replies: 7
    Last Post: 12-18-2011, 05:13 AM
  2. Replies: 2
    Last Post: 09-21-2011, 10:33 AM
  3. Some Exceptions issues
    By fleg in forum New To Java
    Replies: 1
    Last Post: 11-16-2010, 10:10 AM
  4. Exceptions
    By Nerijus in forum New To Java
    Replies: 8
    Last Post: 05-18-2010, 02:44 PM
  5. Replies: 0
    Last Post: 08-26-2009, 02:03 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
  •