SRL Archive - Help and Support

Java help please!


[XoL]

2012-12-05 17:39:34


This is my assignment, very confused on how to actually import export, some assistance would be glady taken.

So far I have just the methods in the assignment slightly written, I am basically just really confused not asking for the entire assignment completed but anything would be great.

Thank you!

Le Jingle

2012-12-05 18:46:55

for the contacts, create a construct class, in which a method stores the ordered (as listed in instructions) data (such as 1st name, last, etc).

could also gather input values for the construct with a method that reads tokens from the inputFileString (could read the ordered tokens and check before writing them to a value in the Contacts construct)?

export would be the same thing except handling the file if name of indicated already exists?

Just some thoughts from a novice in java :)

Cheers,
Lj

Brandon

2012-12-05 18:57:07

Similar to the below, Add some error checking and implement the rest of functions.. Names should be letters only, no spaces in them either. Number should be numbers only.. perhaps separated by spaces or dashes. Email should be in email format? Use a Regex to validate both Email and Phonenumber..

If you know C#, this would be a singe as both are the exact same syntax. Java does not have Get/Set so instead use a function as done below (Already done; Add error checking).

Uhh do the user input and file IO.

Don't get used to us posting code without showing what you've done. I only posted because it was too simple and I believe you can already do most of what I posted.

Take the code below with a grain of salt.. I wrote it in notepad.

Readers (Better to read strings and just try-cast to what is needed.. ints, floats, etc..)..

Personally, I use these (usually ReadConsole or Buffer instead of Scanner):

public String ReadBuffer() throws IOException {
BufferedReader Buffer = new BufferedReader(new InputStreamReader(System.in));
return (Buffer.readLine());
}

public String ReadConsole() {
Console Con = System.console();
return (Con.readLine());
}

public String Read() {
Scanner Reader = new Scanner(System.in);
return (Reader.next());
}

Code..


public class Contact {
private String FirstName, LastName, Address;
private String City, State, Email, Phone;
private int Zip;

public Contact(String FirstName, String LastName, String Address, String City, String State, String Email, String Phone, int Zip) {
this.FirstName = FirstName;
this.LastName = LastName;
this.Address = Address;
this.City = City;
this.State = State;
this.Email = Email;
this.Phone = Phone;
this.Zip = Zip;
}

public String GetFirstName() {
return FirstName;
}

public void SetFirstName(String FirstName) {
this.FirstName = FirstName;
}

public String GetLastName() {
return LastName;
}

public void SetLastName(String LastName) {
this.LastName = LastName;
}

public String GetAddress() {
return Address;
}

public void SetAddress(String Address) {
this.Address = Address;
}

public String GetCity() {
return City;
}

public void SetCity(String City) {
this.City = City;
}

public String GetState() {
return State;
}

public void SetState(String State) {
this.State = State;
}

public String GetEmail() {
return Email;
}

public void SetEmail(String Email) {
this.Email = Email;
}

public String GetPhone() {
return Phone;
}

public void SetPhone(String Phone) {
this.Phone = Phone;
}

public int GetZip() {
return Zip;
}

public void SetZip(int Zip) {
this.Zip = Zip;
}

@Override String toString() {
return null; //Change this..
}
}

public class AddressBook {
private Contact Contacts[];
private int NumberOfContacts = 0;

public AddressBook(int MaxContacts) {
Contacts = new Contacts[MaxContacts];
}

public void search(String Name) {
String LowerName = Name.toLowerCase();

for (int I = 0; I < Contacts.Length; ++I) {
if (Contacts[I].GetFirstName().toLowerCase().equals(LowerName) || Contacts[I].GetLastName().toLowerCase().equals(LowerName)) {
System.out.println(String.valueOf(I) + "\n" + Contacts[I].toString() + "\n\n");
}
}
}

public boolean add() {
if (NumberOfContacts == Contacts.Length) {
return false;
}

//Get User Input here.. Hold it into some Variables.. Pass those to the line below.

Contacts[NumberOfContacts++] = new Contact();
return true;
}

public boolean remove(int Index) {
if (Index >= 0 && Index < Contacts.Length) {
System.out.println(Contacts[Index].toString() + "\n\n");
System.out.println("Are you sure you want to delete this contact? yes/no.");

//Get User input..
String UserInput = null; //Change this to hold the yes or no from the user..

if (UserInput != null && UserInput.toLowerCase().equals("yes")) {
Contacts[Index] = null;
System.out.println("Success: Contact Deleted.");
return true;
}
} else {
System.out.println("ERROR! Invalid Index");
}

return false;
}
}

[XoL]

2012-12-05 21:50:54

Ugh it saddens me you can do that in notepad (This is what I finished in about an hour of work) (And I still didn't do it as well as you did … )



public class Contact {
public String Firstname;
public String Lastname;
public String Address;
public String City;
public String State;
public String Zip;
public String PhoneNum;
public String Email;

public Contact (String Firstname,String Lastname,String Address,String City,String State,String Zip,String PhoneNum,String Email){
this.Firstname = "No name";
this.Lastname = "No name";
this.Address = "No Address";
this.City = "No City";
this.State = "No State";
this.Zip = "No Zip";
this.PhoneNum = "No number";
this.Email = "No email";
}

public Contact (){
this.Firstname = Firstname;
this.Lastname = Lastname;
this.Address = Address;
this.City = City;
this.State = State;
this.Zip = Zip;
this.PhoneNum = PhoneNum;
this.Email = Email;
}


public String getFirstname(){
return Firstname;
}

public void setFirstname(String Firstname){
this.Firstname = Firstname;
}

public String getLastname(){
return Firstname;
}

public void setLastname(String Lastname){
this.Lastname = Lastname;
}

public String getAddress(){
return Address;
}

public void setAddress(String Address){
this.Address = Address;
}

public String getCity(){
return City;
}

public void setCity(String City){
this.City = City;
}

public String getState(){
return State;
}

public void setState(String State){
this.State = State;
}

public String getZip(){
return Zip;
}

public void setZip(String Zip){
this.Zip = Zip;
}

public String getPhoneNum(){
return PhoneNum;
}

public void setPhoneNum(String PhoneNum){
this.PhoneNum = PhoneNum;
}

public String getEmail(){
return Email;
}

public void setEmail(String Email){
this.Email = Email;
}

}


Yeah I am a super beginner still, it is simply not clicking.
Thank you for the help though, much appreciated

Brandon

2012-12-05 22:33:25

From: [XoL]
Ugh it saddens me you can do that in notepad (This is what I finished in about an hour of work) (And I still didn't do it as well as you did … )

Yeah I am a super beginner still, it is simply not clicking.
Thank you for the help though, much appreciated


Meh you can do it too.. Practice.

Uh as for why your code doesn't compile, your constructors are swapped.

It works like this:


EDIT: Oops.. I missed the orange arrow.. Oh well you get the point visually.

That means your default constructor with no parameters should have the values you put (hard coded values) such as no name, no address, no city, etc..

Whereas the one in the picture gets its parameters from the user.. The this keyword is used to specify a variable or function contained within that class, not within the function. So in the above, this.Param is different from Param.

Param is a parameter in the contructor/function. this.Param is a variable within the class. Your assigning the constructor variable to the class variable. We use "this" because both have the same name and it fixes ambiguity.

[XoL]

2012-12-06 15:05:21

Thank you so much both of you, damn do you guys know of any online resources I could learn Java and grasp its concepts better? (I have a month of free time, I am willing to dedicate at least an hour a day… I must be able to program by the time I return :p)

Le Jingle

2012-12-06 15:23:02

TheNewBoston - yt channel,
java e-books - think there's a thread somewhere on here,
buy absolute java/other books online,
orcale docs,
there's a lot of resource :)

[XoL]

2012-12-06 15:30:28

Think Khan academy is a good source for a month? or should I stick with bucky

[XoL]

2012-12-06 16:36:10

Sorry, so I am trying to import and export in my AddressBook Class
I am stuck, so the instructions ask me to have it so that it asks that is checks all the details?

Basically just wrote this

public boolean importContacts (String fileName)
Scanner fileScan = new Scanner (new File("AddressBook.txt"));

Any ideas on how to actually import/export? I have docs to help me do it with binary but I have no idea with text.

Brandon

2012-12-06 18:58:28

From: Echo_

Copy constructor for remove:

public Contact(Contact copy) {
firstName = new String(copy.getFirstName());
lastName = new String(copy.getLastName());
address = new String(copy.getAddress());
city = new String(copy.getCity());
state = new String(copy.getState());
email = new String(copy.getEmail());
phone = new String(copy.getPhone());
zip = copy.getZip();
}



Be careful when suggesting Copy Constructors. He is new so if he goes and implements copy constructors in java without knowing what is immutable, it will screw him as java only copies references. Luckily for him, your copy constructor is using primitive types and immutable structures. Would be nice to put a notice for other new guys reading.




@OP, I do not know if re-sizing is required when importing or if the teacher expects you to wipe out the current contacts and replace them with the imported ones. Thus I implemented re-sizing anyway. Is there any reason that the teacher assigned you such a task without teaching you the basics?

Add these imports to the addressbook class:

import java.io.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import java.util.List;

Add these to your Address book class:


private int EmptyIndex() {
for (int I = 0; I < Contacts.length; ++I) {
if (Contacts[I] == null) {
return I;
}
}
return -1;
}

private void Resize(int NewSize) {
Contact[] ContactArray = new Contact[NewSize];
for (int I = 0; I < Contacts.length; ++I) {
ContactArray[I] = Contacts[I];
}

Contacts = null;
Contacts = ContactArray;
}

public boolean importContacts(String inputFileName) {
try {
List<String> Info = Files.readAllLines(Paths.get(inputFileName), StandardCharsets.UTF_8);
int Index = EmptyIndex();

if (Info.size() >= Contacts.length || Index == -1) {
if (Index == -1 || Info.size() == Contacts.length) {
Resize(Contacts.length + Info.size());
} else {
Resize(Contacts.length + (Info.size() - Contacts.length));
}
}

for (int I = 0; I < Info.size(); ++I) {
String[] Parameters = Info.get(I).split(",");

if (Parameters != null) {
int NewIndex = EmptyIndex();
if (NewIndex != -1) {

for (int J = 0; J < Parameters.length; ++J) {
Parameters[J] = Parameters[J].trim();
}

int Zip = Parameters[5] == null ? 0 : Integer.valueOf(Parameters[5]);
Contacts[NewIndex] = new Contact(Parameters[0], Parameters[1], Parameters[2], Parameters[3], Parameters[4], Parameters[7], Parameters[6], Zip);
}
}
}
for (int I = 0; I < Contacts.length; ++I) {
System.out.println(Contacts[I].toString());
}
} catch (Exception e) {
return false;
}

return true;
}

public boolean export(String outputFileName) {
if (Contacts.length > 0) {
String Info = "";

for (int I = 0; I < Contacts.length; ++I) {
if (Contacts[I] != null) {
Info += Contacts[I].GetFirstName() + ", " + Contacts[I].GetLastName() + ", " + Contacts[I].GetAddress()
+ ", " + Contacts[I].GetCity() + ", " + Contacts[I].GetState() + ", " + Contacts[I].GetZip()
+ ", " + Contacts[I].GetPhone() + ", " + Contacts[I].GetEmail() + "\r\n";
}
}

if (!Info.isEmpty()) {
try {
if (Files.exists(Paths.get(outputFileName), LinkOption.NOFOLLOW_LINKS)) {
System.out.println("The file already exists.. Would you like to overwrite it? yes/no");
BufferedReader Buffer = new BufferedReader(new InputStreamReader(System.in));
String Answer = Buffer.readLine().trim();
if (Answer.toLowerCase().equals("no")) {
return false;
}
}
Files.write(Paths.get(outputFileName), Info.getBytes(), StandardOpenOption.CREATE);
System.out.println("Overwritten");
return true;
} catch (Exception e) {
return false;
}
}
}
return false;
}

Now I tend to write code without comments and sometimes it can get quite complex. I don't know if it is complex to you or not but if it is, drop a line below and either myself or someone would explain it to you.

[XoL]

2012-12-06 20:18:03

Yes please explain, sorry >.<

Brandon

2012-12-06 20:47:15

I tried to explain it with comments below. If you still have questions, copy and post which part you didn't understand and I'll try and explain it in more detail.


/** The following function will find an index in the array that is empty. **/

private int EmptyIndex() {
for (int I = 0; I < Contacts.length; ++I) { //go through the whole contact array.
if (Contacts[I] == null) { //If it a contact doesn't exist at that index, return the index.
return I;
}
}
return -1; //else return -1 because the array is full.
}


/** Resize the contact array to the new size **/

private void Resize(int NewSize) {
Contact[] ContactArray = new Contact[NewSize]; //Create a Contact array of new size.
for (int I = 0; I < Contacts.length; ++I) { //For go through the old array.
ContactArray[I] = Contacts[I]; //Copy all the elements in the old one to the new one.
}

Contacts = null; //Make the old array reference point to nothing..
Contacts = ContactArray; //Make the old reference point to the new array :)
}




public boolean importContacts(String inputFileName) {
try { //We need to try the following because the file may not exist.. Thus we "try" to open it.

List<String> Info = Files.readAllLines(Paths.get(inputFileName), StandardCharsets.UTF_8); //Read the file into a list of strings. Each index in the list holds one line in the file.
//The above reads the file line by line into a list/array. Each index holds one line.

int Index = EmptyIndex(); //Find an empty index in the Contact Array.

if (Info.size() >= Contacts.length || Index == -1) { //If the amount of contacts in the file is larger than our current array, we need to resize ours to hold all of them.
if (Index == -1 || Info.size() == Contacts.length) { //If it is larger than or equal to our array, Increase our array size by the amount needed.
Resize(Contacts.length + Info.size());
} else {
Resize(Contacts.length + (Info.size() - Contacts.length)); //Else increase our array but only the amount needed (the difference in size) to hold the contacts in the file.
}
}

for (int I = 0; I < Info.size(); ++I) { //For every line in the file..
String[] Parameters = Info.get(I).split(","); //Split up each line by Commas. That way we get FName, LName, City, State, etc.. into an array.

if (Parameters != null) { //If our array holds First name, last name, city, etc..
int NewIndex = EmptyIndex(); //Get an empty spot in our Contact array so we can create a new contact using the information from the file.
if (NewIndex != -1) { //If there is space in our contact array for the new contact from the file, lets add them.

for (int J = 0; J < Parameters.length; ++J) {
Parameters[J] = Parameters[J].trim(); //Remove any unnecessary characters such as trailing spaces.
}

int Zip = Parameters[5] == null ? 0 : Integer.valueOf(Parameters[5]); //Zip was read as a string.. We need an integer so lets convert it..

//In the below line, We add the new contact to the end of the contact array :)
Contacts[NewIndex] = new Contact(Parameters[0], Parameters[1], Parameters[2], Parameters[3], Parameters[4], Parameters[7], Parameters[6], Zip);
}
}
}
} catch (Exception e) {
return false; //There was a problem reading the file, return false because we didn't add them to our contact array.
}

return true; //return true, all operations was successful.
}


/** The following function will go through our array, get all the information for each contact and write it to a file. **/

public boolean export(String outputFileName) {
if (Contacts.length > 0) { //If our contact array is not empty..
String Info = ""; //An empty string that will get filled with contact info.

for (int I = 0; I < Contacts.length; ++I) { //For each contact in our Array, Check if it exists and get their info. Add all their info to a string.
if (Contacts[I] != null) {
Info += Contacts[I].GetFirstName() + ", " + Contacts[I].GetLastName() + ", " + Contacts[I].GetAddress()
+ ", " + Contacts[I].GetCity() + ", " + Contacts[I].GetState() + ", " + Contacts[I].GetZip()
+ ", " + Contacts[I].GetPhone() + ", " + Contacts[I].GetEmail() + "\r\n";
}
}

if (!Info.isEmpty()) { //If we have contact info in our string..
try { //Again, we can "TRY" to create a file..

if (Files.exists(Paths.get(outputFileName), LinkOption.NOFOLLOW_LINKS)) { //If the file already exists.. (Don't check for shortcuts)

System.out.println("The file already exists.. Would you like to overwrite it? yes/no"); //Ask the user if to overwrite it or not.
BufferedReader Buffer = new BufferedReader(new InputStreamReader(System.in)); //Create a buffer to hold user input.. Tis' the same as the Scanner.
String Answer = Buffer.readLine().trim(); //Get the user's input and hold it in the string.
if (Answer.toLowerCase().equals("no")) { //Ignore case-sensitivity. If the response was no, return false.
return false;
}
}

//The response was yes.. Lets write our string of contact infos to the file..
Files.write(Paths.get(outputFileName), Info.getBytes(), StandardOpenOption.CREATE); //Try to write it to a file..
System.out.println("Overwritten");
return true; //If we got this far, return true..
} catch (Exception e) {
return false; //There was a problem writing it to the file.. return false..
}
}
}
return false; //Our contact array had no contacts.. Nothing to write so return false..
}

Echo_

2012-12-07 00:40:35

Brandon, you shouldn't read an entire file's contents into memory like that because the file could be huge.

Well I took the time to properly write the AddressBook class after noticing parts of the directions I didn't follow, and it actually ended up being alot easier to understand. So here's the final version:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class AddressBook {
private Contact[] contacts;
private static Scanner scanner = new Scanner(System.in); //because it's used several times

public AddressBook(int size) {
contacts = new Contact[size];
}

public boolean importContacts(String fileName) {
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
int index = 0;
while ((line = reader.readLine()) != null) {
String[] tokens = line.split(",");
int zip;
try {
zip = Integer.parseInt(tokens[5]);
} catch (NumberFormatException e) {
zip = 0;
}

Contact contact = new Contact(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], zip, tokens[6], tokens[7]);
contacts[index++] = contact;
}

reader.close();
return true;
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

public boolean export(String outputFileName) {
try {
File file = new File(outputFileName);
if (file.exists()) {
System.out.print(outputFileName + " already exists. Overwrite? [y/n]: ");
String input = scanner.next();
if (input.equals("n"))
return false;
}

BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for (int i = 0; i < contacts.length; i++) {
writer.write(contacts[i].getFirstName() + ",");
writer.write(contacts[i].getLastName() + ",");
writer.write(contacts[i].getAddress() + ",");
writer.write(contacts[i].getCity() + ",");
writer.write(contacts[i].getState() + ",");
writer.write(contacts[i].getZip() + ",");
writer.write(contacts[i].getPhoneNumber() + ",");
writer.write(contacts[i].getEmail() + ",");
writer.newLine();
}

writer.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

public void search(String name) {
for (int i = 0; i < contacts.length; i++) {
if (contacts[i].getFirstName().toLowerCase().equals(name.toLowerCase())) {
System.out.println(contacts[i]);
System.out.println("index: " + i);
}

if (contacts[i].getLastName().toLowerCase().equals(name.toLowerCase())) {
System.out.println(contacts[i]);
System.out.println("index: " + i);
}
}
}

public boolean add() {
int index = -1;
for (int i = 0; i < contacts.length; i++)
if (contacts[i] == null) {
index = i;
break;
}

if (index == -1) {
System.out.println("Address book is full.");
return false;
}

System.out.print("First name: ");
String firstName = scanner.next();
System.out.print("Last name: ");
String lastName = scanner.next();
System.out.print("Address: ");
String address = scanner.next();
System.out.print("City: ");
String city = scanner.next();
System.out.print("State: ");
String state = scanner.next();
System.out.print("Zip: ");
int zip = scanner.nextInt();
System.out.print("Phone number: ");
String phoneNumber = scanner.next();
System.out.print("Email: ");
String email = scanner.next();
contacts[index] = new Contact(firstName, lastName, address, city, state, zip, phoneNumber, email);
return true;
}

public boolean remove(int id) {
try {
System.out.println(contacts[id]);
System.out.print("Are you sure you want to delete this contact? [y/n]: ");
String input = scanner.next();
if (input.equals("y")) {
contacts[id] = null;
return true;
} else
return false;
} catch (ArrayIndexOutOfBoundsException e) {
return false;
}
}
}

Brandon

2012-12-07 07:19:09

From: Echo_
Brandon, you shouldn't read an entire file's contents into memory like that because the file could be huge.


Good point but I also wouldn't read it like the above either.. Yours will overwrite "Some" Contacts in his address book when importing.

If the address book already contains contacts, it will overwrite as many as it needs. Example: Addressbook contains 50 contacts. Your file contains 25. When you import, your code will overwrite the first 25 with the ones in the file and leave the other 25 hanging around in the array.. It mixes both old and new. It's better to either append them to the old array or completely wipe the old one and replace it with the new one.

Good point on the read by chunks though rather than all at once.

Echo_

2012-12-07 14:47:51

From: Brandon

If the address book already contains contacts, it will overwrite as many as it needs. Example: Addressbook contains 50 contacts. Your file contains 25. When you import, your code will overwrite the first 25 with the ones in the file and leave the other 25 hanging around in the array.. It mixes both old and new.


Then wouldn't you just use a separate AddressBook object for each file read from? Because if you want to add more contacts to an AddressBook already containing contacts you would just use add().

From: Brandon

It's better to either append them to the old array or completely wipe the old one and replace it with the new one.


That's what I thought initially, but the assignment says to use arrays (where a Vector could have been used to easily append contacts) and the process of copying and deleting potentially huge arrays can be time consuming and use a lot of memory.

[XoL]

2012-12-07 15:34:18

Thank you two for all the help! I pretty much used what I knew, and left behind the stuff we weren't taught yet (would look suspicious :p).

Again thank you both so much!
Reps

[XoL]

2012-12-12 01:18:43

Thanks again guys, I got a 90% (I used a bit of both code, and a bit of my own(I used a bit of my own because some of the stuff you guys were using was too advanced and was not taught in class, so I just used some of the methods I knew how to create)
Here is the final code I used

/**
* @Author ------
* 12/6/2012
* Address Book//AddressBook.java
*/
import java.util.Scanner;
import java.io.*;

//calling from the contact class (object)
public class AddressBook {
private Contact Contacts[];
private int NumberOfContacts = 0;
private static Scanner scanner = new Scanner(System.in);

public AddressBook(int MaxContacts) {
Contacts = new Contact[MaxContacts];
}
//importContacts method
public boolean importContacts (String fileName){
Scanner inputStream = null;
try{
inputStream = new Scanner(new File(fileName));
while(inputStream.hasNextLine()){
String[] info = new String[8];
//for loop 8 is the amount of items it asks (Goes through)
for(int i = 0; i < 8; i++){
String user = inputStream.nextLine();
info[i] = user;
}//all of the things it asks for
Contact temp = new Contact(info [0], info [1], info [2], info [3], info [4], info [5], info [6], info [7]);
String user = inputStream.nextLine();
Contact tempContact = new Contact();
System.out.println(user);
}
}
//catches errors if they happen
catch(FileNotFoundException e){
System.out.println("File NOT FOUND");
}
//then closes
inputStream.close();
return false;
}
//exports to a new document
public boolean export(String outputFileName) {
try {
//sends to export.txt in folder
File file = new File("export.txt");
if (file.exists()) {
//asks to overwrite
System.out.print(outputFileName + " already exists. Overwrite? [y/n]: ");
String input = scanner.next();
if (input.equals("n"))
return false;
}
//else it grabs these things and sends to the txt
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for (int i = 0; i < Contacts.length; i++) {
writer.write(Contacts[i].getFirstName() + ",");
writer.write(Contacts[i].getLastName() + ",");
writer.write(Contacts[i].getAddress() + ",");
writer.write(Contacts[i].getCity() + ",");
writer.write(Contacts[i].getState() + ",");
writer.write(Contacts[i].getZip() + ",");
writer.write(Contacts[i].getPhoneNum() + ",");
writer.write(Contacts[i].getEmail() + ",");
writer.newLine();
}

writer.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
//finds name
public void search(String Name) {
String LowerName = Name.toLowerCase();

for (int i = 0; i < Contacts.length; i++) {
if (Contacts[i].getFirstName().toLowerCase().equals(LowerName) || Contacts[i].getLastName().toLowerCase().equals(LowerName)) {
System.out.println(String.valueOf(i) + "\n" + Contacts[i].toString() + "\n\n");
}
}
}
//adds a new contact to the book
public boolean add() {
int index = -1;
for (int i = 0; i < Contacts.length; i++)
if (Contacts[i] == null) {
index = i;
break;
}
//max amount in address book
if (index == -1) {
System.out.println("Address book is full.");
return false;
}

System.out.print("First name: ");
String FirstName = scanner.next();
System.out.print("Last name: ");
String LastName = scanner.next();
System.out.print("Address: ");
String Address = scanner.next();
System.out.print("City: ");
String City = scanner.next();
System.out.print("State: ");
String State = scanner.next();
System.out.print("Zip: ");
String Zip = scanner.next();
System.out.print("Phone number: ");
String PhoneNum = scanner.next();
System.out.print("Email: ");
String Email = scanner.next();
Contacts[index] = new Contact(FirstName,LastName, Address, City,State, Email, PhoneNum, Zip);
return true;
}

//removes someone from the address book via the array
public boolean remove(int id) {
try {
System.out.println(Contacts[id]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ERROR: " + id + " is an invalid index.");
return false;
}

System.out.print("Do you want to remove this contact? [yes/no]");
//Check for the user typing yes (put it in your driver class)
Contact[] temp = new Contact[Contacts.length - 1];
for (int i = 0; i < Contacts.length; i++) {
if (i == id)
continue;
if (i > id)
temp[i - 1] = new Contact(Contacts[i]);
else
temp[i] = new Contact(Contacts[i]);
}
//deletes from the arraycopy (in the contact.java)
Contacts = new Contact[Contacts.length - 1];
System.arraycopy(temp, 0, Contacts, 0, Contacts.length);
return true;
}
}