It would be nice to let the user enter the string to search for. Further hacks you might consider would allow the user to add and delete strings in the array.
But lets do more than that.
Let us make this program an (almost) practical application.
Our application will search for a name and will
then print out that person's phone number.
The list of names and phone numbers is implemented
as a list of PhoneEntry
objects:
class PhoneEntry { String name; // name of a person String phone; // their phone number // constructor PhoneEntry( String n, String p ) { name = n; phone = p; } }
The phone number is a String (not an int
) because no
arithmetic is done with it,
and because it may contain colons and dashes.
The main()
method declares this array:
PhoneEntry[] phoneBook = new PhoneEntry[ 5 ] ;