Advertisement
binibiningtinamoran

AddressBook

Nov 16th, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class AddressBook {
  5.  
  6. public static void main(String[] args) {
  7.  
  8. Scanner scanner = new Scanner(System.in);
  9. System.out.print("Enter number of contacts you want to input: ");
  10. int numberOfContacts = Integer.parseInt(scanner.nextLine());
  11. while (numberOfContacts <= 0) {
  12. System.out.println("Invalid number. Must be positive and greater than 0.");
  13. System.out.print("Enter number of contacts you want to input: ");
  14. numberOfContacts = Integer.parseInt(scanner.nextLine());
  15.  
  16. }
  17. Contact[] contacts = new Contact[numberOfContacts];
  18.  
  19. for (int i = 0; i < contacts.length; i++) {
  20. System.out.print("What is your last name? ");
  21. String last = scanner.nextLine();
  22. System.out.print("What is your first name? ");
  23. String first = scanner.nextLine();
  24. System.out.print("What is your email? ");
  25. String email = scanner.nextLine();
  26.  
  27. contacts[i] = new Contact(last, first, email);
  28. }
  29.  
  30. Arrays.sort(contacts); // this sorts the array of Contact objects according to last name.
  31.  
  32. for (Contact c : contacts) {
  33. System.out.printf("First name: %s\nLast Name: %s\nEmail: %s\n\n",c.getFirstName(),
  34. c.getLastName(), c.getEmail());
  35. }
  36.  
  37. }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement