Solved

Applet error display - checking for duplicate items in doubly linked list

Posted on 2004-03-30
133
344 Views
Last Modified: 2012-06-21
Hello,

I'm having problems checking for duplicates.  In my command line (Win-DOS), it'll print "Duplicate ID, can't add.", but in my applet, it doesn't show that in the error message.  Also, duplicate names are suppose to be ok as long as the IDs are different -- they should be added to the list sorted by ID first, then by name.  I think my program is saying dup names are not ok, and so does not add it to the list.

----------------------------------------------------------------------
My codes:
----------------------------------------------------------------------
1. http://www2.hawaii.edu/~flam/DLN.java
2. http://www2.hawaii.edu/~flam/ISortedList.java
3. http://www2.hawaii.edu/~flam/Person.java
4. http://www2.hawaii.edu/~flam/SortedList.java
5. http://www2.hawaii.edu/~flam/SortedListException.java
6. http://www2.hawaii.edu/~flam/OrderedList.java

Thank you!!

-luna621 =^^=
0
Comment
Question by:luna621
  • 72
  • 42
  • 19
133 Comments
 

Author Comment

by:luna621
ID: 10712071
0
 

Author Comment

by:luna621
ID: 10712075
Oh look!  There's an Edit Question link.  Sorry!!
0
 

Author Comment

by:luna621
ID: 10712082
Ok, I corrected it.  Now, I'm off to a peaceful place.     -_-  ....zzzzzZZZZ
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10712120
You can show an error-message using JOptionPane.showMessageDialog ().
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10712127
>> In my command line (Win-DOS), it'll print "Duplicate ID, can't add.", but in my applet, it doesn't show that in the error message.

At the place where you use a System.out.println () to show the message on the console, use a JOptionPane.showMessageDialog (). Its in the javax.swing package.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10712136
In SortedLis.java:

>> else if (cmpResult==0) {
>> System.out.println("\nSorry! A Person with this ID already exists!\n");

else if ( cmpResult == 0 ) {
  JOptionPane.showMessageDialog ( <parent-frame>, "Sorry! A Person with this ID already exists! Oro! :-) ", "ALERT", JOptionPane.WARNING_MESSAGE ) ; // show a warning message

Mayank =^^=

;-)
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10712144
Pass the Applet as the <parent-frame>. You will need to pass it as an extra parameter somehow to SortedList.java.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10712154
But I guess the compareTo () method has to be slightly shaped up. Because you don't return a 0 in all cases where the IDs are the same.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10712240
Remark:
The removeAll() function of your SortedList is not used for the moment I guess.
Because you would quickly have a null pointer exception.

Mind the memory leaks!
As I told you before, the Garbage Collector can't clean up objects it they are still referenced.

E.g. Your removePos() function also has this problem:
If you delete the first node of the list, you just replace head, by it's successor and decrement the count. That not enough: You have to reset the link of your current head to the previous head too.

That's why (in some previous posts) I adviced you to have a

           public void remove(DLN node)

I see you added it. But hey, you have to use it too. ;)
Please use it in all your other remove functions.

Now, it takes care of
(1) cleaning up obsolete references
(2) count--

You could also change it to take care of resetting head/tail too (if needed)  (3)
Then you never have to break your head on that again.

That's a main rule of thumb: (try to) break up your code in small pieces you can easily manage.
Pro's :
a) They are simple (because they only perform a small part of the job)
b) Once they are working well (=tested), you can rely on them with your eyes closed:
    That means, you can reuse them where needed without asking if it will work.

Here's the new remove():

public void remove(DLN node) {

        // Reset some links
        if(node.getPrev()!=null) {
            node.getPrev().setNext(node.getNext());                                 // (1)
        } // end if
      else {
          // we're deleting the head, so:
          head = head.getNext();                                                          // (3)
        }

        if(node.getNext()!=null) {
           node.getNext().setPrev(node.getPrev());                                 // (1)
        } // end if
      else {
          // we're deleting the tail, so:
          tail = tail.getPrev();                                                               // (3)
        }

        node.setPrev(null);
        node.setNext(null);
        count--;                                                                                 // (2)
    } // end remove()


Now, look how simple your removePos() function can be written:

    public void removePos(int index) throws SortedListException {
        if (index >= 0 && index <= count) {

            DLN nodeToDelete = find(index);
            remove(nodeToDelete);                                        // takes care of (1), (2) & (3)

        } // end if
        else {
            System.out.println("List index out of bounds exception on remove");
        } // end else
    }   // end removePos()

and your removeAll():

public void removeAll() {
    while(head != null)
        remove(head);   // This "automagically" changes head to the next one
}

0
 
LVL 37

Expert Comment

by:zzynx
ID: 10712278
I see you also introduced a find() function.

Well, you can use it to increment the performance of your add function:

replace
             DLN nodeTmp = head;
by
             DLN nodeTmp = find(count/2);

[ You remember the idea behind it, don't you? But you had problems because get() returned Comparable. So, since find() doesn't ... ]

And in your remove() function you certainly also have to replace

             DLN nodeTmp = new DLN(get(count/2));

by

             DLN nodeTmp = find(count/2);

Because, you don't have to compare with a new object [ new DLN(get(count/2)); ]
if the object you need [ find(count/2); ] is available

I realize these are no comments on your actual question,
but I think they make your app much better. And that's the goal, isn't it?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10712299
Another thing:

in your remove(Comparable item)  function

you have the line

             int cmpResult = nodeTmp.compareTo(item);


That's wrong: you compare a DLN object (nodeTmp) with a Person object (item)

If you really run that you will get a ClassCastException at

public int compareTo(Object obj) {
         DLN node = (DLN)obj;                    <<<<<<<<<<<<<<<<<<<<<<<<<
         return getItem().compareTo(node.getItem());
     } // end compareTo()

because the incoming obj is a Person, and can't be casted to a DLN object.

It should be
                     int cmpResult = nodeTmp.compareTo(nodeToX);

comparing DLN objects

A lot of stuff to think about, huh?
;)

If you have questions about my comments, please ask.
0
 

Author Comment

by:luna621
ID: 10719211
Wow, that IS a lot to think about!  Need to let my brain settle for a bit before I post any more comments =^^=
0
 

Author Comment

by:luna621
ID: 10719235
You know, I just realized I already had a:

    public Comparable get(int position) throws SortedListException {
        ...
    } // end get()

I think I'm going to rewrite that with the stuff you said about my find().
0
 

Author Comment

by:luna621
ID: 10719262
>Comment from zzynx
>Date: 03/30/2004 12:17AM HST
>
>Remark:
>The removeAll() function of your SortedList is not used for the moment I guess.
>Because you would quickly have a null pointer exception.
---------------------------------------------------------------------------------------------
Ok, this is the removeAll() I currently have:

    public void removeAll() {
        while(head != null) {
            //temp = head;  <----------- do I need this?
            head = head.getNext();
            temp.setNext(null);
            //head.setPrev(null);  <----------- do I need this?
            temp = null;
        } // end while

        head = null;
        count = 0; // <-------------- I know this is ok  :)

    } // end removeAll()
0
 

Author Comment

by:luna621
ID: 10719285
>Comment from zzynx
>Date: 03/30/2004 12:31AM HST
>
>Another thing:
>
>in your remove(Comparable item)  function
>
>you have the line
>
>             int cmpResult = nodeTmp.compareTo(item);
>
>It should be
>                     int cmpResult = nodeTmp.compareTo(nodeToX);
---------------------------------------------------------------------------
Woopsie!!  I guess I didn't see that.  ^_^''

Okay, I think I'm going to try to use this remove method, and not the removePos method.  Need to update a bit first.
0
 

Author Comment

by:luna621
ID: 10719305
>Comment from zzynx
>Date: 03/30/2004 12:17AM HST
>
>Remark:
>The removeAll() function of your SortedList is not used for the moment I guess.
---------------------------------------------------------------------------------------------
I use it in the OrderedList.java, and it seems to work ok...

    public class Ears implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if(whichButton.equals("Clear List")) {
                if(list.isEmpty()==true) {
                    error.setText("Sorry, there are no people in the list.");
                    id.setEditable(false);
                    name.setEditable(false);
                    add.setEnabled(false);
                    removeFirst.setEnabled(false);
                    removeLast.setEnabled(false);
                    clear.setEnabled(false);
                } // end if
                if(list.isEmpty()==false) {
                   list.removeAll(); //<------------------------------- here it is!!!
                   listArea.setText("");
                } // end if
            } // end if
        } // end actionPerformed
    } // end Ears
0
 

Author Comment

by:luna621
ID: 10719489
Okay, I'm confusing myself - so I deleted some methods.  Basically, my SortedList.java has:

/**
 * add --- Adds the given item to the list in its correct sorted-order position.
 *         Duplicate items (where x.compareTo(y) == 0) may or may not be suppor-
 *         ted, depending on the implementing class. x comes before y in the list
 *         if x.compareTo(y) < 0.
 *
 * @param item, a java.lang.Comparable object
 * @throws SortedListException if the item cannot be added to the list
 */
    public void add(Comparable item) throws SortedListException {
        ....
    } // end add()

/**
 * get --- Gets the object at the given position in the list.  Does not affect
 *         the state of the list.
 *
 * @param position the position of the item to get
 * @return the object in that position in the list
 * @throws SortedListException  if the object cannot be returned
 *         (such as when position is < 1 or > size())
 */
  public Comparable get(int position) throws SortedListException {
        ....
    } // end get()


/**
 * isEmpty --- Determines whether or not the list is empty.
 *
 * @return false if there are no elements in the list (size() == 0),
 *         otherwise returns true.
 */
    public boolean isEmpty() {
        ....
    } // end isEmpty()


/**
 * remove --- Removes the given item from the list.
 *            Whether all matching items or only one are removed depends on the
 *            implementation.
 *
 * @param item, the java.lang.Comparable object to remove from this list.
 * @throws SortedListException if the object cannot be removed
 *         (such as when the item is not in the list)
 */
    public void remove(Comparable item) throws SortedListException {
        ....
    } // end remove()


/**
 * remove --- Remove the object at the given position in the list.  The given
 *            position must be in the range of the list: 1 <= position <= size()
 *
 * @param position the position to remove from the list
 * @throws SortedListException  if the object cannot be removed
 *         (such as when position is < 1 or > size())
 */
    public void remove(int position) throws SortedListException {
        ....
    } // end remove()


/**
 * removeAll --- Removes all elements from this list.  size() is then 0.
 *
 */
    public void removeAll() {
        ....
    } // end removeAll()


/**
 * size --- Returns the size of the list.
 *
 * @return the number of elements in the list
 */
    public int size() {
        ....
    } // end size()
-------------------------------------------------------------------------
So far, I think everything works EXCEPT the 2 remove methods.  Let me try go through it again with your suggestions.  I'm trying not to add anymore methods than I need, but will add if necessary  :)
0
 

Author Comment

by:luna621
ID: 10719628
Ah!  The errors!!

java.lang.ClassCastException
        at Person.compareTo(Person.java:38)
        at DLN.compareTo(DLN.java:67)
        at SortedList.remove(SortedList.java:163)
        at SortedList.remove(SortedList.java:225)

----------------------------------------------------------------------
Updated codes:
----------------------------------------------------------------------
1. http://www2.hawaii.edu/~flam/SortedList.java
2. http://www2.hawaii.edu/~flam/OrderedList.java
0
 

Author Comment

by:luna621
ID: 10719926
Okay rewrote:

    public void remove(int position) throws SortedListException {
        if (position >= 0 && position <= count) {
            DLN nodeToDelete = get(position);
            remove(nodeToDelete);
        } // end if
        else {
            System.out.println("List index out of bounds exception on remove");
        } // end else
    } // end remove()
---------------------------------------------------------------------------------------
Gives this error:

found   : java.lang.Comparable
required: DLN
            DLN nodeToDelete = get(position);
                                  ^
1 error

Here's the get method once again:

    public Comparable get(int position) throws SortedListException {
        DLN current = head;
        for (int skip = 0; skip < position; skip++) {
            current = current.getNext();
        } // end for
        return current.getItem();
    } // end get()
0
 

Author Comment

by:luna621
ID: 10719932
Sorry, the error should look like this:

            DLN nodeToDelete = get(position);
                                               ^
0
 

Author Comment

by:luna621
ID: 10719939
Updated codes:
----------------------------------------------------------------------
1. http://www2.hawaii.edu/~flam/SortedList.java
0
 

Author Comment

by:luna621
ID: 10719998
Ok, I just typecasted it:

            DLN nodeToDelete = ((DLN)get(position));
0
 

Author Comment

by:luna621
ID: 10720012
Nevermind...

java.lang.ClassCastException
        at SortedList.remove(SortedList.java:222)
0
 

Author Comment

by:luna621
ID: 10720701
Okay, changed it back to what I originally had.  And I still get this error:

java.lang.ClassCastException
        at Person.compareTo(Person.java:38)
        at DLN.compareTo(DLN.java:67)
        at SortedList.remove(SortedList.java:154)
        at SortedList.remove(SortedList.java:211)

Is something wrong with my compareTo method?

---------------------------------------------------------
Person.java
---------------------------------------------------------
    public int compareTo(Object object) {
        //typecase to same kind of object
        int iValue = ((Person)object).getID(); // <------------------  at Person.compareTo(Person.java:38)
        //now it is possible to compare
        if(this.getID()<iValue) {
            return -1;
        } // end if
        if(this.getID()>iValue) {
            return 1;
        } // end if
        return 0;
    } // compareTo()

---------------------------------------------------------
DLN.java
---------------------------------------------------------
     public int compareTo(Object obj) {
         DLN node = (DLN)obj;
         return getItem().compareTo(node.getItem()); // <-----------  at DLN.compareTo(DLN.java:67)
     } // end compareTo()

---------------------------------------------------------
SortedList.java
---------------------------------------------------------
    public void remove(Comparable item) throws SortedListException { ;
        if(head==null) { // list is empty, nothing to delete
            System.out.println("Nothing to delete!  List is empty.");
            return;
        } // end if
        DLN nodeToX = new DLN(item); // We make a temporary new DLN object
                                     // for the item we have to delete
        DLN nodeTmp = new DLN(get(count/2));
        int firstCmpResult = nodeTmp.compareTo(nodeToX); // <------------  at SortedList.remove(SortedList.java:154)
        while(true) {
        ...
    } // end remove()


    public void remove(int position) throws SortedListException {
        if(position >= 0 && position <= count) {
            DLN nodeToDelete =  new DLN(get(position));
            remove(nodeToDelete); // <-------------  at SortedList.remove(SortedList.java:211)
        } // end if
        else {
            System.out.println("List index out of bounds exception on remove");
        } // end else
    } // end remove()
0
 

Author Comment

by:luna621
ID: 10720716
My getItem in DLN.java is:

     public Comparable getItem() {
         return cItem;
     } // end getItem

And getID in Person.java is:

    public int getID() {
        return this.iID;
    } // END getID()
0
 

Author Comment

by:luna621
ID: 10720736
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10720876
>> int iValue = ((Person)object).getID();

Perhaps the object passed is not of type Person somehow.

Did you try the JOptionPane for showing the message-boxes?
0
 

Author Comment

by:luna621
ID: 10720919
Hmm... is that the same thing as Java Console??  This is what it prints:

Name added.

Count is: 1

Name added.

Count is: 2

Name added.

Count is: 3
java.lang.ClassCastException
      at Person.compareTo(Person.java:38)
      at DLN.compareTo(DLN.java:67)
      at SortedList.remove(SortedList.java:154)
      at SortedList.remove(SortedList.java:212)
      at OrderedList$Ears.actionPerformed(OrderedList.java:269)
      at java.awt.Button.processActionEvent(Unknown Source)
      at java.awt.Button.processEvent(Unknown Source)
      at java.awt.Component.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)
0
 

Author Comment

by:luna621
ID: 10720927
   public int compareTo(Object object) {
        //typecase to same kind of object
        Person obj = (Person)object; //<------------------- this seems to be the thing giving me problems...
        int iValue = obj.getID();
        //now it is possible to compare
        if(this.getID()<iValue) {
            return -1;
        } // end if
        if(this.getID()>iValue) {
            return 1;
        } // end if
        return 0;
    } // end compareTo()
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10720938
>> Hmm... is that the same thing as Java Console??  This is what it prints:

No. JOptionPane is for displaying message-boxes. It should display the stuff in a customized dialog-box.

>> Person obj = (Person)object; //<------------------- this seems to be the thing giving me problems...

Then your object is not of type Person. This problem was not there yesterday, right? How did it come?
0
 

Author Comment

by:luna621
ID: 10720970
Where would I access the JOptionPane?

>Then your object is not of type Person. This problem was not there yesterday, right? How did it come?


I think I changed my program according to this comment:

Comment from zzynx
Date: 03/30/2004 12:31AM HST


Another thing:

in your remove(Comparable item)  function

you have the line

             int cmpResult = nodeTmp.compareTo(item);


That's wrong: you compare a DLN object (nodeTmp) with a Person object (item)

If you really run that you will get a ClassCastException at

public int compareTo(Object obj) {
         DLN node = (DLN)obj;                    <<<<<<<<<<<<<<<<<<<<<<<<<
         return getItem().compareTo(node.getItem());
     } // end compareTo()

because the incoming obj is a Person, and can't be casted to a DLN object.

It should be
                     int cmpResult = nodeTmp.compareTo(nodeToX);

comparing DLN objects
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10721003
>> Where would I access the JOptionPane?

Exactly where you print: System.out.println ( "That ID already exists" ) ; // or something like that -> where you display that the ID is a duplicate

>> I think I changed my program according to this comment

Oh, then I'll have to go through the code again. Can you just try debugging with a few System.out.println () statements in compareTo () ?

Person.java:

public int compareTo(Object object) {
        //typecase to same kind of object
        System.out.println ( object.getClass () ) ; // print the class of object
        Person obj = (Person)object; //<------------------- this seems to be the thing giving me problems...
        int iValue = obj.getID();
        //now it is possible to compare
        if(this.getID()<iValue) {
            return -1;
        } // end if
        if(this.getID()>iValue) {
            return 1;
        } // end if
        return 0;
    } // end compareTo()
0
 

Author Comment

by:luna621
ID: 10721077
Ah ha!!  I think I know what it is.  In yesterday's program, I used the methods

    public void removePos(int index) throws SortedListException {
        ....
    } // end removePos()
   public void remove(DLN node) {
        ....
    } // end remove()
   public DLN find(int index) {
        ....
    } // end find()



I never used:

public void remove(Comparable item) throws SortedListException {

That's probably why I never had problems with my program.  

In today's code, I removed all the previously listed remove codes and just used the one I didn't use.

0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10721118
Ok, well, you should not *remove* your methods. You should just comment them (I hope you have done that only).

What's the status now?
0
 

Author Comment

by:luna621
ID: 10721208
Oh, I learned that the hard way the other time :)

I saved it as another file.  I just renamed it to the one I'm using for this program, and it works.  It's just that I was hoping to get the:

public void remove(Comparable item) throws SortedListException {
    ...
} // end remove()

to work some how, since my add() is also taking a Comparable item.  The remove() should be similar to the add()... maybe something's wrong with my get().  I was having problems with that.  Hence why I created & am using a find().  Is something wrong with my get()?

    public Comparable get(int position) throws SortedListException {
        DLN current = head;
        for (int skip = 0; skip < position; skip++) {
            current = current.getNext();
        } // end for
        return current.getItem();
    } // end get()
0
 

Author Comment

by:luna621
ID: 10721225
Here's the SortedList.java that I'm using now: http://www2.hawaii.edu/~flam/SortedList.java

This is the original SortedList.java: http://www2.hawaii.edu/~flam/SortedList-original.java
0
 

Author Comment

by:luna621
ID: 10721275
AAAAHHHHHHHHHHHH!!!!  I figured it out!!!!!  It was

    public void remove(int position) throws SortedListException {
        ...
    } // end remove()


that wasn't calling the right thing!!!
0
 

Author Comment

by:luna621
ID: 10721281
Ok, here's the updated code: http://www2.hawaii.edu/~flam/SortedList.java

Yay!  Now I can work on the error display.  I'll be back with more questions soon!

-luna621 =^^=
0
 

Author Comment

by:luna621
ID: 10721339
Oh darn!  I forgot to check the Remove Last button.  Okay, I get this error:

java.lang.NullPointerException
        at SortedList.get(SortedList.java:122)
        at SortedList.remove(SortedList.java:225)

    public Comparable get(int position) throws SortedListException {
        DLN current = head;
        for (int skip = 0; skip < position; skip++) {
            current = current.getNext();
        } // end for
        return current.getItem(); //<--------------- at SortedList.get(SortedList.java:122)
    } // end get()


    public void remove(int position) throws SortedListException {
        if (position >= 0 && position <= count) {
            if(position == 0) {
            // delete the first node from the list
            head = head.getNext();
            } // end if
            else {
                DLN prev = (DLN)get(position); //<---------------  at SortedList.remove(SortedList.java:225)
                if(prev == null) {
                    return ;
                } // end if
                // delete the node after the node that prev
                // references, save reference to node
                DLN current = prev.getNext();
                if(current != null) {
                    prev.setNext(current.getNext());
                } // end if
            } // end else
            count--;
        } // end if
        else {
            System.out.println("List index out of bounds exception on remove");
        } // end else
    } // end remove()
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10721340
Since you're using multiple .java files, make sure your classes don't get mixed up. You should make all your classes public to be on the safe side, since you have only one class in one .java file. Otherwise, you can have a SortedList class in SortedList.java and one SortedList class in SortedList-original.java, and you might compile the second one -> and expect the first one to run :-)
0
 

Author Comment

by:luna621
ID: 10721349
I call the Remove Last from the OrderedList.java as:   list.remove(list.size());
0
 

Author Comment

by:luna621
ID: 10721354
>Comment from mayankeagle
>Date: 03/30/2004 08:46PM HST

Ooo!!  Good point.  Let me check that now :)
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10721358
Do you pass list.size () or list.size () - 1 ??
0
 

Author Comment

by:luna621
ID: 10721363
Yup, I was using the correct .class file  =^^=

Still getting the NullPointerException though  >_<''
0
 

Author Comment

by:luna621
ID: 10721367
Let me try list.size()-1.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10721378
Try with size () - 1.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10721382
:-)
0
 

Author Comment

by:luna621
ID: 10721385
java.lang.ClassCastException
        at SortedList.remove(SortedList.java:225)

    public void remove(int position) throws SortedListException {
        if (position >= 0 && position <= count) {
            if(position == 0) {
            // delete the first node from the list
            head = head.getNext();
            } // end if
            else {
                DLN prev = (DLN)get(position); //<------------- at SortedList.remove(SortedList.java:225)
                if(prev == null) {
                    return ;
                } // end if
                // delete the node after the node that prev
                // references, save reference to node
                DLN current = prev.getNext();
                if(current != null) {
                    prev.setNext(current.getNext());
                } // end if
            } // end else
            count--;
        } // end if
        else {
            System.out.println("List index out of bounds exception on remove");
        } // end else
    } // end remove()


Should I write an 'if' statement in there that handles.... ???  Something that'll fix the null pointer?
0
 

Author Comment

by:luna621
ID: 10721402
java.lang.ClassCastException

That's strange, since the Remove First works...
0
 

Author Comment

by:luna621
ID: 10721408
BTW, I call Remove First like this:   list.remove(0);
0
 

Author Comment

by:luna621
ID: 10721415
I checked my counters, and they are counting nicely.  And since:

    public int size() {
        return count;
    } // end size()

that method should be ok...
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10721419
>> Something that'll fix the null pointer?

Its a ClassCastException, right? Not a NullPointer anymore?

I have a feeling that the classes have been mixed up. Can you just make all of them public and re-compile? That could resolve some issues.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10721426
>> BTW, I call Remove First like this:   list.remove(0);

Then removeLast () should be passed size () - 1.
0
 

Author Comment

by:luna621
ID: 10721476
ok, I'm going to recompile everything.  Hold on  =^^=
0
 

Author Comment

by:luna621
ID: 10721484
Ok, everything is recompiled, but I still get that ClassCastException...
0
 

Author Comment

by:luna621
ID: 10721492
That's funny.  When I call it list.remove(list.size()), I get null pointer.  When I call list.remove(list.size()-1), I get ClassCastException.
0
 

Author Comment

by:luna621
ID: 10721538
   public void remove(int position) throws SortedListException {
        if (position >= 0 && position <= count) {
            if(position == 0) {
                // delete the first node from the list
                head = head.getNext();
            } // end if
            else {
                DLN prev = (DLN)get(position);
                if(prev == null) {
                    return;
                } // end if
                // delete the node after the node that prev
                // references, save reference to node
                DLN current = prev.getNext();
                if(current != null) {
                    prev.setNext(current.getNext());
                } // end if
            } // end else
            count--;
        } // end if
        else {
            System.out.println("List index out of bounds exception on remove");
        } // end else
    } // end remove()

//-----------------------------------------------------------------
                if(prev == null) {
                    return;
                } // end if

Is that correct?
0
 

Author Comment

by:luna621
ID: 10721574
Okay, should be something like:

    public void remove(int position) throws SortedListException {
        if (position >= 0 && position <= count) {
            if(position == 0) {
                // delete the first node from the list
                head = head.getNext();
            } // end if
            else {
                DLN prev = (DLN)get(position);
                if(prev == null) {
                    return;
                } // end if
                // delete the node after the node that prev
                // references, save reference to node
                DLN current = prev.getNext();
                if(current != null) {
                    prev.setNext(current.getNext());
                } // end if
            } // end else
            count--;
        } // end if
        else {
            // position = count
            ...
        } // end else
    } // end remove()
0
 

Author Comment

by:luna621
ID: 10721577
Oh wait.  Nevermind.   >_<''
0
 

Author Comment

by:luna621
ID: 10721681
   public void remove(int position) throws SortedListException {
        ...
                DLN prev = (DLN)get(position);
        ...
    } // end remove()

    public Comparable get(int position) throws SortedListException {
        ...
    } // end get()

If I'm sending size(), which is count, an integer... I get a null pointer.  If I'm sending size()-1, I get a ClassCastException.  Why is that?
0
 

Author Comment

by:luna621
ID: 10721690
   public Comparable get(int position) throws SortedListException {
        DLN current = head;
        for (int skip = 0; skip < position; skip++) {
            current = current.getNext();
        } // end for
        return current.getItem();
    } // end get()

Should I add an 'if' statement somewhere in there

if(position is the last node) {
   do something;
} // end if
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10721711
Hey luna,

I'm back in.
Forgive me if I say, but I find it a pitty you don't read the comments in more detail.

(1) Why do you still use the get() function that returns a Comparable if you have such a nice find() function that returns nicely a DLN

Why writing:

         DLN prev = (DLN)get(position);
or
         DLN nodeToDelete = get(position);

if you can write

         DLN prev = find(position);
or
         DLN nodeToDelete = find(position);

Please use find()!!!

(2) Why do you keep writing such an extended remove(int position) function if it can be written
as
public void remove(int position) throws SortedListException {
        if (position >= 0 && position <= count) {
            DLN nodeToDelete = find(position);
            remove(nodeToDelete);
        } // end if
        else {
            System.out.println("List index out of bounds exception on remove");
        } // end else
} // end remove()

The same code as your previous removePos() function. You may remove functions, but keep the way they were working.

>>  the removeAll() seems to work ok...
I didn't say it wasn't, but it had memory leaks

0
 

Author Comment

by:luna621
ID: 10721743
Basically, I need to use:

    public Comparable get(int position) throws SortedListException {

-not-

    public DLN find(int index) {

Though I wish I could, since it's easier  :)

And I need to use:

    public void remove(Comparable item) throws SortedListException {

-not-

    public DLN remove(DLN nodeToRemove) {


Arrrgghh!!   >_<''
0
 

Author Comment

by:luna621
ID: 10721753
>(2) Why do you keep writing such an extended remove(int position) function if it can be written

I think I changed it because the get() wasn't working in there.  Once I have the get() working properly, I'll change it back  :)
0
 

Author Comment

by:luna621
ID: 10721774
   public Comparable get(int position) throws SortedListException {
        DLN current = head;
            for (int skip = 0; skip < position; skip++) {
                current = current.getNext(); //<---------------- do I need to check if getNext() = null?
            } // end for
            return current.getItem();
    } // end get()
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10721778
>> Why do you still use the get() function that returns a Comparable if you have such a nice find() function

Correct. You don't need both. Use only one of them.

>> If I'm sending size(), which is count, an integer... I get a null pointer.  If I'm sending size()-1, I get a ClassCastException.  Why is that?

Because when you send size (), you go one ahead of the last node. So before it can cast, it throws a NullPointer. When you send size () - 1, you reach the correct node (so there is no NullPointer), but there is a casting-problem.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 30

Expert Comment

by:mayankeagle
ID: 10721786
>> current = current.getNext(); //<---------------- do I need to check if getNext() = null?

That would arise if you pass a value for position which is >= size (). To be on the safe side, you can check. But I guess that you will not be passing such a value anywhere in your program.
0
 

Author Comment

by:luna621
ID: 10721807
>Because when you send size (), you go one ahead of the last node. So before it can cast, it throws a NullPointer. When
>you send size () - 1, you reach the correct node (so there is no NullPointer), but there is a casting-problem.

Is there a way to correct this?
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10721832
>> Is there a way to correct this?

If you handle with a check -> whether getNext () returns null or not, then the NullPointer can be handled.

For the class-cast, well - the correct object has to be passed.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10721861
Well, if you can't use find() then make get() work as find() ;)

   public Comparable get(int position) throws SortedListException {
        DLN current = head;
        for (int skip = 0; skip < position; skip++) {
            current = current.getNext();
        } // end for
        return current;  // instead of current.getItem()
    } // end get()

I don't understand why you return

           current.getItem();

In your SortedList class you're dealing with DLN objects. Forget about what's in it.
Work with DLN objects. And since you made your DLN class implementing the Comparable interface it's OK for the return type.

Of course, don't forget to check all your code for this new return object.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10721871
>> Forget about what's in it.
I mean in the DLN objects
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10721887
I still see:

DLN nodeTmp = new DLN(get(count/2));

Make that (with your new get()):

DLN nodeTmp = get(count/2);

0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10721900
>> return current;  // instead of current.getItem()

I think that should correct the ClassCast. Perhaps that is why it was throwing it. And I never noticed that this method should return a DLN !
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10721910
Also in your add()

DLN nodeTmp = head;         ====>   DLN nodeTmp = get(count/2);

You see, how easy things become?

By the way:

>>And I need to use:
>>
>>    public void remove(Comparable item) throws SortedListException {
>>
>>-not-
>>
>>    public DLN remove(DLN nodeToRemove) {

You need to use remove(Comparable item).
That's OK. [ Remember item being a DLN object, not a Person object what's in it. If you have a Person object P and want to call remove(P), well then call remove(new DLN(P)); ]

But *how* you implement that remove(Comparable item) is your business. No?
Well, who forbids you to have remove(Comparable  item) call your own remove(DLN node) function that nicely takes care of (1), (2) & (3)? Nobody!
Just do it.


0
 

Author Comment

by:luna621
ID: 10721921
Okaaaaay!!!  Re-introduced the find(), and everything is working nicely.  Now I need to test a bit more...

0
 
LVL 37

Expert Comment

by:zzynx
ID: 10721930
>> I think that should correct the ClassCast. Perhaps that is why it was throwing it.
Sure. Saw it already, but always forget to mention. There's so much comments here... ;)

So: read my first comments again containing the removeXXX() codes.
      and replace find() by get()

0
 
LVL 37

Expert Comment

by:zzynx
ID: 10721950
>> Okaaaaay!!!  Re-introduced the find(), and everything is working nicely
But you say you have to use get()???
Anyway, whatever you use, find() or get() or yodelahey() *make sure it returns a DLN object*!!!
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10721973
>> Re-introduced the find(), and everything is working nicely

Perhaps the modified get () looked better.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10721990
>> everything is working nicely
Don't forget my comments about the memory leaks!
That's something you don't *see*, even if all is working nicely.
0
 

Author Comment

by:luna621
ID: 10721991
Forgive me!!  I'm confusing everyone including myself  ^_^''

Okay, one last question (I hope).

I'm trying to make sure duplicate IDs are not added.  I know the add() checks that already, but I also want my applet to display the error msg: "Duplicate ID.  Could not be added."  I was thinking of making a checkDup():

    public int checkDup(Comparable person,int iIDInput) {
        DLN node = new DLN(person);
        DLN nodeTmp = head;
        // need to traverse list and compare to each node???
        int newID = node.compareTo(person);
            if(newID == iIDInput) {
                return 0;
            } // end if
        return -1;
    } // end checkDup()

Then call it in my applet as:
                               ...
                                Person person = new Person(nameInput,iIDInput);
                                    if(list.checkDup(person, iIDInput)!=0) {
                                        list.add(person);
                                        listArea.setText("");
                                        list.printList(listArea);
                                    } // end if
                                    else {
                                        error.setText("Duplicate ID.  Can't add to list.");
                                    } // end else
                                 ...
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722026
I have already told you about that - use the JOptionPane.showMessageDialog ().
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722031
>> error.setText("Duplicate ID.  Can't add to list.");

Use the JOptionPane there.
0
 

Author Comment

by:luna621
ID: 10722044
>Pass the Applet as the <parent-frame>. You will need to pass it as an extra parameter somehow to SortedList.java.

How do I pass the applet?  Do I pass the name of the applet?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10722047
Why even not use JOptionPane.showMessageDialog() in your add() function

instead of

System.out.println("\nSorry! A Person with this ID already exists!\n");

>> I know the add() checks that already, but I also want my applet to display the error msg: "Duplicate ID.  Could not be added."
Well,

           JOptionPane.showMessageDialog()

in your add() function should work
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10722051
It's *never* a good idea to have the same logic (checking for double IDs) in two or more functions (add() & checkDup())

Because, when you change one, you always have to change the other functions too.
And then you forget one and you can go bug hunting.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722053
>> How do I pass the applet?  Do I pass the name of the applet?

If you're calling it in your applet, pass this.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722058
>> System.out.println("\nSorry! A Person with this ID already exists!\n");

It might be better to remove all System.out.println () statements now that you are working with Applets. But display the error message only once - not in the checkDup () or add () method as well as the Applet (it would be better to display in the applet, because then you can pass 'this', otherwise, you need to pass it somehow as an extra parameter to checkDup () or add () for displaying).
0
 

Author Comment

by:luna621
ID: 10722059
           ...
            else if (cmpResult==0) {
                System.out.println("\nSorry! A Person with this ID already exists!\n");
                JOptionPane.showMessageDialog(); // show a warning message
                break;
            } // end if
            ...

Do I need to import or extend something, because I get this error:

symbol  : variable JOptionPane
location: class SortedList
                JOptionPane.showMessageDialog(); // show a warning message
                ^
1 error
0
 

Author Comment

by:luna621
ID: 10722061
Oh, I should write that in the applet class??
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722066
impot javax.swing.JOptionPane ;
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722076
>> Oh, I should write that in the applet class??

Yes, write it in the applet - the place where you do. error.setText ( "...." ) ;

JOptionPane.showMessageDialog ( this, "Sorry! A Person with this ID already exists! Oro! :-) ", "ALERT", JOptionPane.WARNING_MESSAGE ) ;
0
 

Author Comment

by:luna621
ID: 10722086
symbol  : method showMessageDialog (OrderedList.Ears,java.lang.String,java.lang.String,int)
location: class javax.swing.JOptionPane
                                error.setText(JOptionPane.showMessageDialog(this, "Sorry! A Person with this ID already exists!  
                                                                                         Oro! :-) ", "ALERT", JOptionPane.WARNING_MESSAGE));
                                                                       ^
1 error

This is what I put:

error.setText(JOptionPane.showMessageDialog(this, "Sorry! A Person with this ID already exists! Oro! :-) ", "ALERT", JOptionPane.WARNING_MESSAGE));
0
 

Author Comment

by:luna621
ID: 10722090
Do I need to write a new method in the applet class that passes (String, String, int)??
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10722107
>> Oh, I should write that in the applet class??
Indeed, that's mayankeagle's (good) proposal.
Because showMessageDialog() needs some parameters that are not available in your SortedList class but in your applet.

So that's why we propose:

1) don't introduce a checkDup()
2) don't System.out.println(...) in add() but throw an exception instead
    [ This way you'll really "use" the "throws SortedListException" part of add() ]
3) catch that exception in your applet and if it occurs perform a showMessageDialog() with the right message
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10722117
Hey,

NOT

error.setText(JOptionPane.showMessageDialog(this, "Sorry! A Person with this ID already exists! Oro! :-) ", "ALERT", JOptionPane.WARNING_MESSAGE));

but

JOptionPane.showMessageDialog(this, "Sorry! A Person with this ID already exists! Oro! :-) ", "ALERT", JOptionPane.WARNING_MESSAGE);

0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722125
Remove error.setText ();
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722141
See the 3rd comment that I posted on this page at the start.
0
 

Author Comment

by:luna621
ID: 10722150
Ok, I removed it but it still gives me:

symbol  : method showMessageDialog (OrderedList.Ears,java.lang.String,java.lang.String,int)
location: class javax.swing.JOptionPane
                                error.setText(JOptionPane.showMessageDialog(this, "Sorry! A Person with this ID already exists!  
                                                                                         Oro! :-) ", "ALERT", JOptionPane.WARNING_MESSAGE));
                                                                       ^
1 error
0
 

Author Comment

by:luna621
ID: 10722153
Sorry, once more:

symbol  : method showMessageDialog (OrderedList.Ears,java.lang.String,java.lang.String,int)
location: class javax.swing.JOptionPane
                                JOptionPane.showMessageDialog(this, "Sorry! A Person with this ID already exists! Oro! :-                                                                                ) ", "ALERT", JOptionPane.WARNING_MESSAGE);
                                                    ^
1 error
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722165
>> this

Oh, its referring to the inner-class.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722171
Where do you instantiate the Ears class? Pass the Applet to it as an argument and store it in a member of the Ears class. Then use that member instead of 'this'.
0
 

Author Comment

by:luna621
ID: 10722179
Ok, instead of 'this', what should I put?

>See the 3rd comment that I posted on this page at the start.

I pasted that in the add(), and it gave:

1.  illegal start of expression
                JOptionPane.showMessageDialog ( <parent-frame>, "Sorry! A Person with this ID already exists! Oro! :-
                                                                                            ) ", "ALERT", JOptionPane.WARNING_MESSAGE ) ; // show
                                                                                            a warning message
                                                                       ^
2.  ')' expected
                JOptionPane.showMessageDialog ( <parent-frame>, "Sorry! A Person with this ID already exists! Oro! :-
                                                                 ) ", "ALERT", JOptionPane.WARNING_MESSAGE ) ; // show a warning message
                                                                                                                                       ^
2 errors
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10722180
I think we better stop posting with the speed of light... ;)
luna, I'm gonna let this last part to mayankeagle.
Our concurrent posts confuse you. And that's not the goal.

See you at the accept.
bye
0
 

Author Comment

by:luna621
ID: 10722185
Okay, Ears is located in OrderedList.java (my applet class):

    public class Ears implements ActionListener {
        ...
    } // end Ears

>Pass the Applet to it as an argument and store it in a member of the Ears class.

Do you mean:

    public class Ears(Applet applet) implements ActionListener {
        ...
    } // end Ears
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722188
Actually, you can give a try to:

JOptionPane.showMessageDialog ( e.getSource ().getParent (), "Sorry! A Person with this ID already exists! Oro! :-) ", "ALERT", JOptionPane.WARNING_MESSAGE);

e.getSource () would be the button and .getParent () of that will give you the Applet :-)
0
 

Author Comment

by:luna621
ID: 10722191
Bye zzynx!  Thank you for your help!!  =^^=

See you soon!!  :)
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722207
Or perhaps: e.getSource ().getParent ().getParent ()

Just noticed that the button is in a Panel, so that would be its parent, and the parent of the Panel would be the Applet.

>> public class Ears(Applet applet) implements ActionListener

No. The constructor should be like:

public class Ears implements ActionListener
{
  private Applet applet ;

  public Ears ( Applet applet )
  {
    this.applet = applet ;
  }

  public void actionPerformed ( .... )
  {
    ..
    JOptionPane.showMessageDialog ( applet, "Sorry! A Person with this ID already exists! Oro! :-) ", "ALERT", JOptionPane.WARNING_MESSAGE);

  }

}
0
 

Author Comment

by:luna621
ID: 10722208
1.  cannot resolve symbol
symbol  : method showMessageDialog (OrderedList.Ears,java.lang.String,java.lang.String,int)
location: class javax.swing.JOptionPane
                                JOptionPane.showMessageDialog(this, "Sorry! A Person with this ID already exists! Oro! :-) ", "ALERT", JOptionPane.WARNING_MESSAGE);
                                           ^
2.  cannot resolve symbol
symbol  : variable e
location: class SortedList
                JOptionPane.showMessageDialog(e.getSource().getParent(), "Sorry! A Person with this ID already exists! Oro! :-) ", "ALERT", JOptionPane.WARNING_MESSAGE ) ; // show a warning message
                                              ^
2 errors

This is what I have in the add():

            else if (cmpResult==0) {
                JOptionPane.showMessageDialog(e.getSource().getParent(), "Sorry! A Person with this ID already exists! Oro! :-
                ) ", "ALERT", JOptionPane.WARNING_MESSAGE ) ; // show a warning message
                break;
            } // end if

And this is in the Ears class:

              JOptionPane.showMessageDialog(this, "Sorry! A Person with this ID already exists! Oro! :-) ", "ALERT",
                     JOptionPane.WARNING_MESSAGE);
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722218
Don't put it in add (). Put:

>> JOptionPane.showMessageDialog(e.getSource().getParent(), "Sorry! A Person with this ID already exists! Oro! :-
                ) ", "ALERT", JOptionPane.WARNING_MESSAGE ) ;

in Ears.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722224
Like I already said, you don't need to put it in 2 locations. Putting it in the Applet itself is enough (I mean - putting it in the actionPerformed () method of Ears).
0
 

Author Comment

by:luna621
ID: 10722230
Oops!!  Sorry!!  Let me try that!
0
 

Author Comment

by:luna621
ID: 10722243
1.  cannot resolve symbol
symbol  : method getParent ()
location: class java.lang.Object
                                JOptionPane.showMessageDialog(e.getSource().getParent(), "Sorry! A Person with this ID already exists! Oro!", "ALERT", JOptionPane.WARNING_MESSAGE ) ;
                                                                         ^
2.  cannot resolve symbol
symbol  : variable e
location: class SortedList
                JOptionPane.showMessageDialog(e.getSource().getParent(), "Sorry! A Person with this ID already exists! Oro! :-) ", "ALERT", JOptionPane.WARNING_MESSAGE ) ; // show a warning message
                                              ^
2 errors
-------------------------------------------------------------
I thought I defined the variable 'e' as:    public void actionPerformed(ActionEvent e)
0
 

Author Comment

by:luna621
ID: 10722259
Or do I need to assign 'e' to a value like how I did with:  String whichButton = e.getActionCommand();
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722284
( ( Button ) e.getSource () ).getParent ().getParent ()
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722297
e.getSource () returns an Object, so you need to type-cast it to Button so that you can use the getParent () method.

>> location: class SortedList

You STILL have it in the SortedList class. Remove it from there. Like I said - put it ONLY in the Ears class.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722309
>> Or do I need to assign 'e' to a value like how I did with:  String whichButton = e.getActionCommand();

No. That's not needed. You don't need thae JOptionPane in the SortedList class. The JVM will generate the event and send it to the actionPerformed () method of the Ears class.
0
 

Author Comment

by:luna621
ID: 10722331
try {
                                Person person = new Person(nameInput,iIDInput);
                                list.add(person);
                                listArea.setText("");
                                list.printList(listArea);
                                JOptionPane.showMessageDialog(((Button)e.getSource()).getParent().getParent(), "Sorry! A Person with this ID already exists! Oro!", "ALERT", JOptionPane.WARNING_MESSAGE ) ;
                             } // end try
                            catch (SortedListException ex) {

Ok, right now I have it in the try, so it pops up everytime I add a person.  If I wanted it to pop up only if the IDs are equal, where do I put it?  I tried moving it to the catch area, but nothing pops up.
0
 

Author Comment

by:luna621
ID: 10722333
>You STILL have it in the SortedList class. Remove it from there. Like I said - put it ONLY in the Ears class.

Must have forgotten about that one.  Sorry!!!  ^_^''
0
 

Author Comment

by:luna621
ID: 10722341
Do I need to put something here:

            else if (cmpResult==0) {
                break;

in the add() to tell it to pop a message?
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722409
>> in the add() to tell it to pop a message?

No. That's the whole idea. You're not supposed to pop up a message there. You have to pop up a message in the Applet. That's why I said - put it onl in Ears. Let this:

>> else if (cmpResult==0) {
>> break;

- stay as it is.

>> If I wanted it to pop up only if the IDs are equal, where do I put it?

Do you not have the checkDup () method anymore? If you have it, then wherever you call it from the Applet, if it returns a 0 - then you should pop up the message.

>> I tried moving it to the catch area, but nothing pops up

That is wrong logic. Control will go to the catch only if there is an exception and if there is an exception, then you don't need to display 'that' message :-)
0
 

Author Comment

by:luna621
ID: 10722445
Ok, I added this:

    public boolean popUp() {
        if(popUp==true) {
            return true;
        }
        return false;
    } // end popUp()
----------------------------------------------------------------------------
And this from the add():

            ...
            else if (cmpResult==0) {
                popUp = true;
                break;
           ...
----------------------------------------------------------------------------
And this from the applet class:

                           try {
                                Person person = new Person(nameInput,iIDInput);
                                if(list.popUp()==true) {
                                    JOptionPane.showMessageDialog(((Button)e.getSource()).getParent().getParent(), "Sorry! A
                                    Person with this ID already exists! Oro!", "ALERT", JOptionPane.WARNING_MESSAGE ) ;
                                } // end if
                                if(list.popUp()==false) {
                                    list.add(person);
                                    listArea.setText("");
                                    list.printList(listArea);
                                } // end if
                             } // end try

0
 

Author Comment

by:luna621
ID: 10722455
Only thing is, I have click the "Add" button on the same ID twice before the pop-up pops up.
0
 

Author Comment

by:luna621
ID: 10722484
Also, once the message appears... it'll keep appearing even if I add a different ID number.
0
 
LVL 30

Accepted Solution

by:
mayankeagle earned 500 total points
ID: 10722485
Luna, wait  wait.... don;t go about adding things just like that. You didn't tell me - are you using the checkDup () method anymore or not?

Should I assume that you are not?

>> if(list.popUp()==true) {

It will not have popUp = true unless control goes to: >> else if (cmpResult==0) { popUp = true;

Maybe you can still try this:

Make popUp = false in all if-else cases, other than: >> else if (cmpResult==0) {

try {
  Person person = new Person(nameInput,iIDInput);
  list.add(person); // -> THIS WILL SET popUp = true if there is a duplicate
  if(list.popUp()==true) {
    JOptionPane.showMessageDialog(((Button)e.getSource()).getParent().getParent(), "Sorry! A
      Person with this ID already exists! Oro!", "ALERT", JOptionPane.WARNING_MESSAGE ) ;
  } // end if
  listArea.setText("");
  list.printList(listArea);
} // end try
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722501
>> Make popUp = false in all if-else cases, other than: >> else if (cmpResult==0) {

Only in that method.
0
 

Author Comment

by:luna621
ID: 10722520
HAHAHAHahahaha.... stupid me!  I forgot to add the person to the list first.  Okay, it works now.  Thank you (x3)!!

Woo!!  12:30 am!!  Better get some shut eye.  

I have learned many new things today.  Thank you again!

-luna621 =^^=
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722548
Did you accept only my answer? I guess zzynx also helped you enough. You should give him some points in another question "Points for zzynx" and post a link to that question on this one so that he can go there and collect them.

Good night ;-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10722622
mayankeagle, thanks for your honesty.
I'm indeed rather disappointed luna didn't split up the points.
:(
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722651
Welcome ;-)

>> I'm indeed rather disappointed luna didn't split up the points

I guess it was just in a hurry because (s)he wanted to go to catch some shut eye. But as far as I have seen, (s)he is one of the most honest questioners and active questioners, so I guess by tomorrow, you should get your points in another Q :-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10722734
>> But as far as I have seen, (s)he is one of the most honest questioners and active questioners
I agree with that.

>> so I guess by tomorrow, you should get your points in another Q
That's what I think/expect too.
0
 

Author Comment

by:luna621
ID: 10790092
Sorry, I haven't been checking this lately.  Yup, you got me good.  I was sleepy and I guess I forgot to split points... you know I usually do :)

zzynx, check the posting place again :)

-luna621 =^^=
0
 

Author Comment

by:luna621
ID: 10790204
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10790250
Thanks luna!
CU
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapBully challenge 6 93
mapShare challenge 13 69
micro services vs rest web services 16 53
Basic Java Case or If-Else statement... 3 13
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now