Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4538582
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:49:51+00:00 2026-05-21T14:49:51+00:00

I’m creating a very simple phone directory for a homework assignment but I’m stuck.

  • 0

I’m creating a very simple phone directory for a homework assignment but I’m stuck. I’ve tried looking through similar threads, and while those have been useful they haven’t answered my question. I have a class name Directory which has an ArrayList that is compiled from another class called Person. The Person class gathers the information about the contact: First & Last Name, Phone Number and Email addresses of all the contacts. I’m trying to read the ArrayList size from another class: _MainMenu so that if I want to add another contact, the method addNewPerson can be dynamic. I’ll include the code for the MainMenu and Directory. Basically I’m trying to avoid having to create a new object for the class Person by hand because I don’t know how many contacts the user will have. I hope that makes sense… Sorry if it doesn’t. I’ll try to clarify as questions undoubtedly come in.

import java.util.Scanner;

public class _MainMenu {
public static Scanner input = new Scanner(System.in);
public static int choice;
public static String firstName = new String();
public static String lastName = "";
public static String phoneNumbers;
public static String emailAddresses;
public static Person pI = new Person ();

public static void main(String[] args) throws Exception
{
    Directory d1 = new Directory("myDir.txt");
    do
    {
    printMenu();
    selectSubMenu();
    if(choice == 1)
    {
        for(int i = 0; i < d1.getSize(); i++)
        d1.addNewPerson(pI);
    }
    }while(choice != 3);
    d1.writeToFile("myDir.txt");
}

public static void printMenu()
{
    System.out.printf("%s\n%s\n%s\n%s\n",
            "Select The Number Of Your Choice: ",
            "1 - Add New Contact",
            "2 - Search (Display/Edit/Remove)",
            "3 - Exit");
}

public static void selectSubMenu()
{
    choice = input.nextInt();
    switch(choice)
    {
        case 1:
            addNewContact();
            break;
        case 2:
//              search();
            break;
        case 3:
            break;
        default:
            System.out.println("Invalid selection. Please try again.");
    }

}

public static void addNewContact()
{
    System.out.println("Please enter the first name: ");
    firstName = input.next();
    System.out.println("Please enter the last name: ");
    lastName = input.next();
    System.out.println("Please enter up to three phone numbers: ");
    phoneNumbers += input.next();
    System.out.println("Please enter up to three email addresses: ");
    emailAddresses += input.next();

}

}

And here’s the code for class Directory:

import java.util.ArrayList;
import java.util.Formatter;
import java.io.File;
import java.util.Scanner;

public class Directory {

private ArrayList <Person> directory = new ArrayList <Person>();

public Directory(String name) throws Exception
{
    File f = new File(name);
    Scanner input;
    if(f.exists())
    {
        input = new Scanner (f);
        while(input.hasNext())
        {
            Person p = new Person();
            p.setFname(input.next());
            p.setLname(input.next());
            int pNumber = input.nextInt();
            for (int i=0; i< pNumber; i++)
                p.setPhone(input.next());
            int eNumber = input.nextInt();
            for (int i=0; i< eNumber; i++)
                p.setemail(input.next());
            directory.add(p);
        }
        input.close();
    }
    else
        f.createNewFile();
}
public Person find(String fName, String lName)
{
    for(int i=0; i<directory.size(); i++)
    {
        if (directory.get(i).getLname().equals(lName) && directory.get(i).getFname().equals(fName))
            return directory.get(i);
    }
    return null;
}

public void addNewPerson(Person p)
{
    directory.add(p);
}
public void writeToFile(String name) throws Exception
{
    Formatter inFile = new Formatter(name);
    for( int i =0; i< directory.size(); i++){
        inFile.format("%s %s %d ", directory.get(i).getFname(),directory.get(i).getLname(),directory.get(i).pNum);
        for(int j=0; j<directory.get(i).pNum; j++)
            inFile.format("%s ",directory.get(i).getPhones()[j]);
        inFile.format("%d ", directory.get(i).eNum);
        for(int j=0; j<directory.get(i).eNum; j++)
            inFile.format("%s ",directory.get(i).getemails()[j]);
    }
    inFile.close();
}
}

I understand that the code in MainMenu within the if(choice == 1) statement doesn’t work, but I can’t think of how to do this. I want to have Person pI to be the dynamic variable so that if the user wants to add another contact, then he selects “Add Contact” and the program will how many contacts are already in the Directory ArrayList and it will add ‘1’ then input that new variable into “d1.addNewPerson(pI)” so that it works something like this: “d1.addNewPerson(d1.size() + 1)”… I don’t know if any of that even made sense. Anyway if someone can make sense of it and know how to work the code, I would greatly appreciate it. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T14:49:52+00:00Added an answer on May 21, 2026 at 2:49 pm

    You are going to have to create a new Person object for each entry.

    Person p = new Person();
    p.firstname = ... 
    p.lastname = ..
    
    d1.addNewPerson(p);
    

    and to get the size of the ArrayList, you can try d1.directory.size() since director is a public field. But if possible you should make it private and add a method to Directory

    public int getSize()
    {
        return directory.size();
    }
    

    EDIT:

    you can modify your code to something like this

    if(choice == 1)
    {
            Person p = new Person();
            p.firstname = ... 
            p.lastname = ..
    
    
            d1.addNewPerson(pI);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.