Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class AddressBook {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter number of contacts you want to input: ");
- int numberOfContacts = Integer.parseInt(scanner.nextLine());
- while (numberOfContacts <= 0) {
- System.out.println("Invalid number. Must be positive and greater than 0.");
- System.out.print("Enter number of contacts you want to input: ");
- numberOfContacts = Integer.parseInt(scanner.nextLine());
- }
- Contact[] contacts = new Contact[numberOfContacts];
- for (int i = 0; i < contacts.length; i++) {
- System.out.print("What is your last name? ");
- String last = scanner.nextLine();
- System.out.print("What is your first name? ");
- String first = scanner.nextLine();
- System.out.print("What is your email? ");
- String email = scanner.nextLine();
- contacts[i] = new Contact(last, first, email);
- }
- Arrays.sort(contacts); // this sorts the array of Contact objects according to last name.
- for (Contact c : contacts) {
- System.out.printf("First name: %s\nLast Name: %s\nEmail: %s\n\n",c.getFirstName(),
- c.getLastName(), c.getEmail());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement