This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serching for a surname in my collection

 
Samuel Campbell
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about a loop? In pseudo code:
 
Samuel Campbell
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your previous post you mentioned not using == but using equals which I misunderstood for the assignment operator =


 
Samuel Campbell
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Complied and run but does not work! Throws a null pointer exception but the surName is not null
 
Samuel Campbell
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic