• Post Reply Bookmark Topic Watch Topic
  • New Topic

Address book?  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys, I am trying to build an address book, and I started from what i know how to do.




Now I would like the program to take a string I insert on command line (for example java Cerca"Robert Baratheon") and compare it to the nome field of every Persona in the address book. But I have no idea on how. Could any of you give me some suggestion, please?
 
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you planning to compare just the name or the Persona object?

For the name only, you can do something like:


I shall let you figure out the other approach (compare the Persona object)
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware: A cellulare is not an int. For a start it begins with 0 and the 0 is essential, which is inconsistent with an int.
 
Pier Maccà
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so must I write a string for every new Persona I put in? There isn' t a way to make the program compare every nome of every Persona object already in? It says to me that i Cannot make a static reference to the non-static field Persona : |
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So seems like you need to use the object comparison.

Well you can do this:


Now how to write the equals method for object Persona? Since you only concern about the name, use this only for a start.



When writing the equals method, the hashcode method is also needed using the same variables ("name" in your case).

IDE can help generate the equals and hashcode methods.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure I understand about writing a new String? You can put the Persona objects into an array and iterate the array looking for a match by name. Or you can use a List<Persona> to iterate.

The ideal for an address book is a probably Map<Nome, Persona>, but you might not know about Maps and Lists yet.
 
Pier Maccà
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Array thing! I will try it, thank you! I do not know about Maps and Lists, but I will : )
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

Two Java® Tutorials sections which I think might be useful: 1 2.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!