• 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

Creating an Array of Objects in a Constructor

 
Greenhorn
Posts: 5
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

I want to create a simple app that takes a name from the console then compares the name to a small phone book,when the name matches another name it will return the associated phone number.

I have a small contacts class which has name and number fields,Then I have a phone book class which populates an array with 4 contact objects that I can compare the entered number against.

here is my contacts class




and here is my attempt at the phonebook class



In the main method I am just trying to print out one of the fields for one contact to see if I can actually access it to compare it to the name entered.Its saying "MaryJones" cannot be resolved to a type.I'm guessing I cant create all that code in the constructor?Can anyone give me a hand on how it should look?
Thanks in advance

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alan Flynn wrote:Can anyone give me a hand on how it should look?


Sure. Try this as a start:(I'll leave you to fill in the rest)
I think you'll find that works a lot better.

Now, the question is why?

Perhaps you can tell us?

Winston
 
Alan Flynn
Greenhorn
Posts: 5
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Winston Thanks for the quick reply!

I definitely want to try and fill it out myself!Its the only way I will learn

OK so from what I see you moved the creation of the contactList out of the Constructor?
Hmm..So does this mean the array will be created first then populated by the constructor after??
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alan Flynn wrote:OK so from what I see you moved the creation of the contactList out of the Constructor?
Hmm..So does this mean the array will be created first then populated by the constructor after??


No, it means that it is now part of the object; not part of the constructor. And that means that it can be accessed by any method IN the object.

You're worrying too much about mechanics at the moment.

Winston
 
Ranch Hand
Posts: 146
2
Mac Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following on from that, can you think of a way to access the name of MaryJane without declaring and initialising the variable in the class scope? If you think about what you are doing in the constructor and the affect of the change Winston suggested I think you should be able to come up with the solution. Let us know how you get on.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic