my dog learned polymorphism*
The moose likes Beginning Java and the fly likes help me to correct the code:searchForEntry Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help me to correct the code:searchForEntry " Watch "help me to correct the code:searchForEntry " New topic
Author

help me to correct the code:searchForEntry

Alex cordes
Greenhorn

Joined: Nov 15, 2005
Posts: 15
i wrote these codes, and what i want is: when i search for a date, it displays all the people present in that date... ...but now it only displays one people... ...(i actually put 2-3 people into that date)
so how can i improve my program???
thanks a lot~~~


[ November 16, 2005: Message edited by: Alex cordes ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

indexOf only returns the first index so that's not a possibility.
What you need is a loop, that continues until nothing is found.

There is no indexOf that starts at a given position, so iterating over all elements is the only option (well, that is what indexOf does until it finds a result):
Or, alternatively:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Hint: The indexOf method is overridden in the String class.

One version is indexOf(String str), which you can use to find the first occurrence. If this is -1, then there aren't any.

Another version is indexOf(String str, int fromIndex), which you can use to find all additional occurrences after the first. Just put this in a while(index != -1) loop.

EDIT: I just assumed that you're searching a String, but I guess you didn't specify what type "dates" is in the above code, so I don't know...
[ November 16, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Alex cordes
Greenhorn

Joined: Nov 15, 2005
Posts: 15
Originally posted by Rob Spoor:
indexOf only returns the first index so that's not a possibility.
What you need is a loop, that continues until nothing is found.

There is no indexOf that starts at a given position, so iterating over all elements is the only option (well, that is what indexOf does until it finds a result):
Or, alternatively:


thanks for ur reply, but when i try to run urs, sth wrong with "for (Iterator i = dates.iterator(); i.hasNext(); index++) {"

"cannot resolve symbol - class Iterator"
???what does that mean?
Alex cordes
Greenhorn

Joined: Nov 15, 2005
Posts: 15
oh i get it... ...but can u explan a bit more about it?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Ok, so you've imported java.util.Iterator

Now for the explanation:
Pretty straight forward - check the Iterator API for more information.

Then I noticed that dates and names are matching - dates.get(i) coincides with names.get(i) for all i within the range of both Lists.
Therefore, we can iterate over both Lists simultaneously:


By the way, this structure is not safe: if you're using multiple threads than you need to keep access to both Lists atomic (synchronized).

You may want to reconsider your structure - a mapping from dates to Lists of persons might do the trick:
Of course, these blocks should be synchronized as well.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Originally posted by marc weber:
Hint: The indexOf method is overridden in the String class.
(PS: you mean overloaded )
Alex cordes
Greenhorn

Joined: Nov 15, 2005
Posts: 15
Originally posted by Rob Spoor:
Ok, so you've imported java.util.Iterator

Now for the explanation:
Pretty straight forward - check the Iterator API for more information.

Then I noticed that dates and names are matching - dates.get(i) coincides with names.get(i) for all i within the range of both Lists.
Therefore, we can iterate over both Lists simultaneously:


By the way, this structure is not safe: if you're using multiple threads than you need to keep access to both Lists atomic (synchronized).

You may want to reconsider your structure - a mapping from dates to Lists of persons might do the trick:
Of course, these blocks should be synchronized as well.



so why "for (Iterator i = dates.iterator(), j = names.iterator(); i.hasNext() && j.hasNext(); ) {"

cant be compiled?:" cannot resolve symbol-class Iterator"

by the way, before this codes, i ve made:" private KeyboardInput in = new KeyboardInput();
private ArrayList names = new ArrayList();
private ArrayList dates = new ArrayList();"

thanks for ur explanation~~~
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Have you imported java.util.Iterator yet? Would be a great partner for the java.util.ArrayList import
Alex cordes
Greenhorn

Joined: Nov 15, 2005
Posts: 15
Oh~~~yes, u r right~~~thanks a lot~~~
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Rob Spoor:
(PS: you mean overloaded )

Doh! I certainly do.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help me to correct the code:searchForEntry
 
Similar Threads
Arrays - How do I show the array index postion?
Address Book Java Issues
Help on String methods!!!
string indexes
anyone help me!!!~~~