This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Serching for a surname in my collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Serching for a surname in my collection" Watch "Serching for a surname in my collection" New topic
Author

Serching for a surname in my collection

Samuel Campbell
Greenhorn

Joined: Apr 18, 2010
Posts: 10
Hi, this is my first post so I would first like to introduce myself.
My name is Spangle1187 and I am studying a module called Writing Software which is based on OO and Java.

I am working on a project to create a address book. So for I have created a contact class to hold the relevant data:


And I have created a Contact List Class to which I can add contacts:


The problem I am having is how do I write a method that will search the list of contacts for a surName and then return the details of that contact?


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

How about a loop? In pseudo code:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Samuel Campbell
Greenhorn

Joined: Apr 18, 2010
Posts: 10
That looks to be a nice simple approach. I should have mentioned in my post that I have Googled searching collections and just got bamboozled by complex (well complex for me at the moment) code full of sorting then performing binary searches.

I will have a go at implementing a for loop, thanks
Samuel Campbell
Greenhorn

Joined: Apr 18, 2010
Posts: 10
Is this along the right lines?




I have an error at compile time which is "cannot return a value from method whose result type is void?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

1) never use == for comparing objects, including Strings. Use the equals method instead.
2) You have defined your method to return void. Check the difference with my example.
3) You're missing a final return statement that will be called if none of the contacts matches the surname.
Samuel Campbell
Greenhorn

Joined: Apr 18, 2010
Posts: 10
Sorry for that school boy error, should have read your post properly!

How does this look, I have changed the == to = but this is now causing the compile time error


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

I've told you the exact method you should call. I never mentioned using = instead of ==.

Also, line 12 returns a String that is not compatible with Contact.
Samuel Campbell
Greenhorn

Joined: Apr 18, 2010
Posts: 10
In your previous post you mentioned not using == but using equals which I misunderstood for the assignment operator =


Samuel Campbell
Greenhorn

Joined: Apr 18, 2010
Posts: 10
I am trying to implement this code with the .equals but the compiler is saying that it cannot find variable surName but as far as I can see it is there?



Also with line 12 I was trying to display a message answer that the search could not return any results. Is this not the place to do this and if so how could I return a message saying that the search result was null.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

There are two ways:
1) return null, then in the method that calls this method check if the return value is null (== null and != null are allowed to check for null), then display a message there
2) throw an exception, perhaps a NoSuchElementException. Check out this section of the Java tutorial
Samuel Campbell
Greenhorn

Joined: Apr 18, 2010
Posts: 10
Thanks I will look at the links for exception handling!
Your input has really helped

Any ideas why the code is saying that it cannot find variable surName? as far as I can see it is there?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2982
    
    9
Look closely at the error message. Exactly which line and which column is it pointing to? Which reference to surName is it complaining about?

My guess is it's pointing to the use of this.surName. Ask yourself: what class is "this" at this point in the code?
Samuel Campbell
Greenhorn

Joined: Apr 18, 2010
Posts: 10
Mike Simmons wrote:Look closely at the error message. Exactly which line and which column is it pointing to? Which reference to surName is it complaining about?

My guess is it's pointing to the use of this.surName. Ask yourself: what class is "this" at this point in the code?


Of course thanks buddy was looking at the wrong class. Could not see the wood for the trees!
Samuel Campbell
Greenhorn

Joined: Apr 18, 2010
Posts: 10
Complied and run but does not work! Throws a null pointer exception but the surName is not null
Samuel Campbell
Greenhorn

Joined: Apr 18, 2010
Posts: 10
Removed this and it is now working. Needs a little tweaking in terms of format of output but its working so thanks to both of you for input!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serching for a surname in my collection
 
Similar Threads
Project Help
Pesudo coding
Hibernate Newbie - 1 to Many Relation Tutorial
Project Help
Using text fields to update a selection list