• 0

[JAVA] Help with Address Book


Question

I'm wondering if anyone could help me with an assignment. Basically we have to create an address book in Java using contacts from a file. The ability to search through the contacts and pull up all relevant data is necessary. The contacts are stored in the format below:

name

address

mobile number

home number

name

address

...

I am having difficulty with importing the data into a workable format for an address book. I've tried using classes but I can't seem to get it working. I don't know whether to use a set of array lists or try using location markers for a string array (i.e use blocks 0 -3 for one contact, 4 - 7 to another one etc)

Any help would be much appreciated, thanks in advance

John.

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Hmm, what do you mean using classes?

From what I can see each attribute is in a separate line so just use the .nextLine() function of the Scanner and store that into the corresponding field. I recommend you to create a data structure with fields for the different attributes and create one of these objects for each contact.

Link to comment
Share on other sites

  • 0
The ability to search through the contacts and pull up all relevant data is necessary.

In order to come up with a simple implementation, we'd need to know what you can search against. Can you search only names, or search against anything, i.e. name, address, phone number, etc?

Link to comment
Share on other sites

  • 0
In order to come up with a simple implementation, we'd need to know what you can search against. Can you search only names, or search against anything, i.e. name, address, phone number, etc?

I need to search across all fields, and pull up the relevant data associated.

What I've done so far is to create 4 array lists and populate them with relevant data, so each index location matches to the contact overall ie name[1] = mr bloggs, address[1] = mr blogg's address etc. What I need to do is to find a way of searching for a matching result, and then pull up the index location of all the other array lists involved.

Thanks again!

Link to comment
Share on other sites

  • 0

I'd create a class called contact, which would store strings of name, address etc.

Then create a variable array of contacts called AddressBook which would store the contact objects.

With that method you'd only have to search/use one array.

Link to comment
Share on other sites

  • 0

You would need to loop though the array of all the contacts and see if each contact's info matches the search string. You can use indexOf() to see if the search string is in the info and if it is, you should know the index (counter for your loop) and be able to output that to the screen.

Click here for an example of indexOf() for searching in a string.

Link to comment
Share on other sites

  • 0
I need to search across all fields, and pull up the relevant data associated.

What I've done so far is to create 4 array lists and populate them with relevant data, so each index location matches to the contact overall ie name[1] = mr bloggs, address[1] = mr blogg's address etc. What I need to do is to find a way of searching for a matching result, and then pull up the index location of all the other array lists involved.

Thanks again!

Another question: When you're searching, are you prompting the user what to search against, i.e. "Enter name to search against: " etc etc, or just "Enter search keyword: " and that keyword could match some name OR some address?

The former would be an easier implementation. For example, if searching against name:

In this case, use linear search. Loop through one array, and if find a result, use the counter and grab the data from the other array, so:

String nameKeyword; //Your code to grab input from the user to search against
int count = 0;
for (count = 0; count < name.length; count++)
{
	if (name[count].equals(nameKeyword))
	{
		  System.out.println("Name: " + name[count]);
		  System.out.println("Address: " + address[count]);
		  System.out.println("Home Number: " + homeNumber[count]);
	}
}

Link to comment
Share on other sites

  • 0

Also, does your search have to do a full match or is a partial match enough?

EXAMPLE:

if one of the stored names is "James Ferguson", which of the following searches will match?

"James Ferguson"

"James Ferguso"

"James"

"Ferguson"

"mes Ferg"

"me"

"gus"

"J"

"m"

""

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.