I'm suppose to do this using sorting algorithm..so it asks to creat a friends database application that maintains a file of friend objects that contain names, telephone numberrs, and email adresses..etc. The friends application should load friend records from a file and then allow the user to add new friends, delete friends, display a list of all friends by either first naem or last name, and search for a friend. When The program runs it displays the menu, where add a friend(prompts the user to enter a name) and then delete, search(removes a friend off the list) and stuff...

The program is not done yet.... and I think I should seperate this into two files. can someone help me correct this, like the methods and constructors so that at least it compiles. ...

import java.util.Scanner; 
import java.util.Math;
import java.util.ArrayList; 

public class FriendsMain { 
  public static void main(String[] args) { 
    String friend; 
    String lastName; 
    String firstName; 
    String telephone;
    String email; 
    Scanner input = new Scanner(System.in); 
    ArrayList = new ArrayList(); 
    
   
    do { 
      System.out.println("\n1. Add a friend. "); 
      System.out.println("2. Display friends by last name. "); 
      System.out.println("3. Display friends by first name. ");  
      System.out.println("4. Find a friend"); 
      System.out.println("5. Delete a friend"); 
      System.out.println("6. Quit"); 
      
      choice = input.nextInt(); 
      
      if (choice == 1) { 
        System.out.println("Enter a friend by their first name"); 
        firstName = input.nextLine(); 
        friendList.add(firstName); 
        System.out.println("Enter a friend by their last name: "); 
        lastName = input.nextLine(); 
        friendList.add(lastName); 
        System.out.println("Enter their telephone number: "); 
        telephone = input.nextLine(); 
        telephoneList.add(telephone); 
        System.out.println("Enter their email address: "); 
        email = input.nextLine(); 
        emailList.add(email); 
      } else if ( choice == 2) { 
        friendList.remove(lastName); 
      } else if (choice == 3) { 
        displayArray(friendList); 
      } else if ( choice == 4) { 
        System.out.println(" Enter a friend to search for: ");
        searchName = input.nextInt(); 
      } else if ( choice == 5) { 
        friendList.remove(friend); 
      } while (!= 6) 
    } 
  } 
}

thank you!

Recommended Answers

All 3 Replies

You don't add friends here:

System.out.println("Enter a friend by their first name"); 
        firstName = input.nextLine(); 
        [B]friendList.add(firstName); [/B]
        System.out.println("Enter a friend by their last name: "); 
        lastName = input.nextLine(); 
        [B]friendList.add(lastName); [/B]
        System.out.println("Enter their telephone number: "); 
        telephone = input.nextLine();
        [B]telephoneList.add(telephone);[/B]

You simply add the info of one friend. How on earth are you going to read that from the list?

Create a Friend class/object, and when you read from the keyboard instantiate such an object and put that inside. Also for the delete you need to ask the user the name of the friend you want to delete, then search the list to find where that friend is and then remove it. Look the API for the ArrayList. With your way you remove the element that has the same value as the last value entered to the 'lastname' variable: friendList.remove(lastName);

You don't add friends here:

System.out.println("Enter a friend by their first name"); 
        firstName = input.nextLine(); 
        [B]friendList.add(firstName); [/B]
        System.out.println("Enter a friend by their last name: "); 
        lastName = input.nextLine(); 
        [B]friendList.add(lastName); [/B]
        System.out.println("Enter their telephone number: "); 
        telephone = input.nextLine();
        [B]telephoneList.add(telephone);[/B]

You simply add the info of one friend. How on earth are you going to read that from the list?

Create a Friend class/object, and when you read from the keyboard instantiate such an object and put that inside. Also for the delete you need to ask the user the name of the friend you want to delete, then search the list to find where that friend is and then remove it. Look the API for the ArrayList. With your way you remove the element that has the same value as the last value entered to the 'lastname' variable: friendList.remove(lastName);

I know the "need to ask the user the name of the friend you want to delete, then search the list to find where that friend is and then remove it" I don't know how to do that part...

I know the "need to ask the user the name of the friend you want to delete, then search the list to find where that friend is and then remove it" I don't know how to do that part...

"need to ask the user the name of the friend you want to delete"; // use the Scanner class
String name;

"then search the list to find where that friend is" // use a for loop
int index;

"then remove it" // search the ArrayList API

And it is better to start by creating a Friend class; start by doing one thing at the time.
First try to keep adding friends to the list and have another option that just displays them(no sorting for starters). When you are done with that, move on with the rest.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.