开发者

How to find size of ArrayList from separate class in Java?

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. 开发者_JAVA百科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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜