0

I'm trying to make a phone book reader for a school project. The program asks for a first name and a last name. The names and phone numbers of people are stored in an array. If you don't enter a first name and the last name is assigned to multiple people, the program should spit out multiple peoples' phone numbers. Sorry, it might be a little long.

public static void main(String[] args) {

    //Where the user input starts, gotta get dat number
    Scanner scan = new Scanner(System.in);

    System.out.print("First Name? ");
    String first = scan.nextLine();

    System.out.print("Last Name? ");
    String last = scan.nextLine();

    String fullname = first + last;

    int[] count = new int[0];
    int counter=0;

    if(first.equals("quit") || last.equals("quit"))
        System.out.println("Bye");
    else
    {
        for(int l=0; l < 5; l++)
        {
            PhoneBook pb = new PhoneBook();
            PhoneEntry entry = pb.search(fullname);
            if( entry != null)
            {
                count[counter] = l; //this is where the issue lies
                counter++;
            }
        }

    }

    if(count.length != 0)
    {
        for(int x=0; x<count.length; x++ )
        {
            PhoneBook pb = new PhoneBook();
            System.out.println("Name: " + pb.phoneBook[count[x]].name + " Number: " + pb.phoneBook[count[x]].phone);
            x++;
        }
    }
}

class PhoneEntry
{
    String name;    // name of a person
    String phone;   // their phone number
    PhoneEntry( String n, String p )
    {
        name = n; phone = p;
    }
}

class PhoneBook
{ 
    PhoneEntry[] phoneBook; 
    PhoneBook()    // constructor
    {
        phoneBook = new PhoneEntry[ 5 ] ;
        phoneBook[0] = new PhoneEntry( "James Barclay", "(418)665-1223" ); 
        phoneBook[1] = new PhoneEntry( "Grace Dunbar",  "(860)399-3044" );
        phoneBook[2] = new PhoneEntry( "Paul Kratides", "(815)439-9271" );
        phoneBook[3] = new PhoneEntry( "Violet Smith",  "(312)223-1937" );
        phoneBook[4] = new PhoneEntry( "John Smith",     "(913)883-2874" );
    }

    PhoneEntry search( String targetName )  
    {
        for (int j=0; j<phoneBook.length; j++)
        {
            if ( phoneBook[ j ].

                    //stuff is to make it so that it only tests if its to upper case and if it contains the target
                    name.toUpperCase().contains( targetName.toUpperCase()))
                return phoneBook[ j ];
        }
        return null;
    }
}

Any help is much appreciated.

nalzok
  • 14,965
  • 21
  • 72
  • 139
Moeed Ali
  • 3
  • 2
  • Possible duplicate of [java dynamic array sizes?](http://stackoverflow.com/questions/1647260/java-dynamic-array-sizes) – fabian Mar 06 '16 at 01:26
  • Also http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it – fabian Mar 06 '16 at 01:26

1 Answers1

0

You wrote:

 count[counter] = l; //this is where the issue lies

Actually, the real issue lies here:

 int[] count = new int[0];

You have created the array with size zero; i.e. you can't put any elements into to. If you try to do that, you will get an exception. Presumably that's what is happening ...

In Java, arrays have fixed sizes. You create them with a given size, and they stay that size ... for ever.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • how do i make it so that it gets bigger as I add more and more values? And Thanks for the reply! – Moeed Ali Mar 06 '16 at 02:03
  • You can't. The only way to "make a array bigger" is to make a new array. If you need an "array like" data structure whose size can change, it is better to use a `List` rather than an array. – Stephen C Mar 06 '16 at 02:13