Results 1 to 5 of 5
Thread: Printing Information in a node
- 04-30-2009, 04:48 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
Printing Information in a node
Ok, We've got an assignment, where you have to print names in a particular month.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.
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
- 04-30-2009, 05:17 AM #2
Firstly this
is equivalent to the much simplerJava Code:UnorderedLinkedList<ExtPerson>.LinkedListIterator< ExtPerson> i = list.iterator(); while(i.hasNext()) { //do stuff with i.next() }
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:for (ExtPerson ep : list) { //do stuff with ep }
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
- 04-30-2009, 05:21 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
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.
- 04-30-2009, 05:37 AM #4
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
That worked. Thanks
- 04-30-2009, 05:43 AM #5
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
-
How to add JCheckbox as a node in JTree
By shajuantony in forum javax.swingReplies: 0Last Post: 04-09-2009, 07:19 AM -
XML Node.getNodeValue Problem
By mindscape777 in forum XMLReplies: 1Last Post: 01-11-2009, 02:22 PM -
How to disabled a node.
By smartsubroto in forum New To JavaReplies: 32Last Post: 07-01-2008, 07:30 AM -
Node selection in jtree
By Preethi in forum AWT / SwingReplies: 4Last Post: 06-19-2008, 11:25 PM -
How to Transmit data from one node to another
By swimberl in forum NetworkingReplies: 2Last Post: 01-04-2008, 08:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks