• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serching for a surname in my collection

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about a loop? In pseudo code:
 
Samuel Campbell
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic