Results 1 to 5 of 5
  1. #1
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default Printing Information in a node

    Solved.
    The access that was being used to get to things was backasswards.
    To many .nexts and not enough veriables.
    I'll be sure to tell the people in the class about it.
    Ok, We've got an assignment, where you have to print names in a particular month.

    Yes we've done the assignment with arrays and I got that program working fine. Now we're using a linked list. and I have a basic code someone else wrote and have been using it as a jumping base and help to set up my own algorithem and such things.

    Now, I have sucessfully gotten most of the program working. The problem is this...

    public void printMonth(int Month)
    {
    UnorderedLinkedList<ExtPerson>.LinkedListIterator< ExtPerson> i = list.iterator();
    //using month make a loop
    while(i.hasNext())
    {
    if(i.next().getMonth()== Month)
    {
    System.out.println(i.next().getLastName()+ " " + i.next().getFirstName());
    }
    }

    What happens is insted of printing the first and last name of the person who's birthday is in that month, its printing four different names from four different nodes

    and I have tried changing .next to .info sense info is the name of the half of the node that has the actuall information in it but then it doesn't complie.

    So I'm asking for any suggestions as to what to do insted. I realize part of what its doing is it's going to the information in the link beyond the one its supposed to be doing, I figured that out with the straight up print method. This one though has me stumped.
    Last edited by Tenn; 04-30-2009 at 05:36 AM. Reason: SOlved

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Firstly this
    Java Code:
    UnorderedLinkedList<ExtPerson>.LinkedListIterator< ExtPerson> i = list.iterator();
    while(i.hasNext()) {
        //do stuff with i.next()
    }
    is equivalent to the much simpler
    Java Code:
    for (ExtPerson ep : list) {
        //do stuff with ep
    }
    The problem you have is that every time you call i.next() it will advance the iterator by one. So the next() you print with is not the next() you tested for the date. Using the for-each loop above will solve that problem. To fix it while still using iterators:

    Java Code:
    while(i.hasNext()) {
        ExtPerson ep = i.next();
        if(ep.getMonth() == Month) {
            System.out.println(ep.getLastName()+ " " + ep.getFirstName());
        }
    }
    Last edited by OrangeDog; 04-30-2009 at 05:27 AM. Reason: alternative solution
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    Mmm Thanks, I'll try that and see if it creates the needed result.

    Alright, I put the for each loop in there insted of the crazy blurb. And JGrasp gave me foreach not applicable to expression type --

    Its a linked list, though I dont' see why that should change anything. save Perhaps make the classes that more unwilling to work... I've been fighting with this all day so Its really begining to grate on my last nerve. and at this point all begining to blur togeather.
    Last edited by Tenn; 04-30-2009 at 05:32 AM.

  4. #4
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    That worked. Thanks

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    for-each loops should work with any array and anything that implements Iterable
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. How to add JCheckbox as a node in JTree
    By shajuantony in forum New To Java
    Replies: 0
    Last Post: 04-09-2009, 07:19 AM
  2. XML Node.getNodeValue Problem
    By mindscape777 in forum XML
    Replies: 1
    Last Post: 01-11-2009, 02:22 PM
  3. How to disabled a node.
    By smartsubroto in forum New To Java
    Replies: 32
    Last Post: 07-01-2008, 07:30 AM
  4. Node selection in jtree
    By Preethi in forum AWT / Swing
    Replies: 4
    Last Post: 06-19-2008, 11:25 PM
  5. How to Transmit data from one node to another
    By swimberl in forum Networking
    Replies: 2
    Last Post: 01-04-2008, 08:48 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
  •