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 4030706
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:30:21+00:00 2026-05-20T11:30:21+00:00

I have here a program that is supposed to begin with an array of

  • 0

I have here a program that is supposed to begin with an array of length 1, allow the user to make an entry into the array, and then double the size of the array with each entry the user makes to avoid an java.lang.ArrayIndexOutOfBoundsException error. The way the code is written below, the computer skips else if after the second user entry and goes straight for Sorry, database is full. If I add newRecords = records in the first else if block, I get the java.lang.ArrayIndexOutOfBoundsException error.

public class PhoneDirectory5 {
    public static void main(String args[]) {
        **PhoneRecord[] records= new PhoneRecord[1];
        int numRecords = 0;**

        // Display list of commands
        System.out.println("Phone directory commands: \n" +
                " a - Add a new phone number\n" +
                " f - Find a new phone number\n" +
                " q - Quit\n" +
                " d - Delete record\n");

        // Read and execute commands
        while (true) {

            // Prompt user to enter a command
            SimpleIO.prompt("Enter command (a, f, d, or q): ");
            String command = SimpleIO.readLine().trim();

            // Determine whether command is "a", "f", "q", or
            // illegal. Execute command if illegal.
            **if (command.equalsIgnoreCase("a"))** {

                // Command is "a". prompt user for name and number,
                // then create a phone record and store it in the
                // database.
                **if (numRecords < records.length) {
                    SimpleIO.prompt("Enter a new name: ");
                    String name = SimpleIO.readLine().trim();
                    SimpleIO.prompt("Enter new phone number: ");
                    String number = SimpleIO.readLine().trim();
                    records[numRecords] =
                            new PhoneRecord(name, number);
                    numRecords++;
                } else if (numRecords == records.length) {
                    PhoneRecord[] newRecords = new PhoneRecord[records.length*2];
                    System.arraycopy(records, 0, newRecords, 0, records.length);
                    SimpleIO.prompt("Enter a new name: ");
                    String name = SimpleIO.readLine().trim();
                    SimpleIO.prompt("Enter new phone number: ");
                    String number = SimpleIO.readLine().trim();
                    newRecords[numRecords] =
                            new PhoneRecord(name, number);
                    numRecords++;**
                } else
                    System.out.println("Sorry, database is full.");

            } else if (command.equalsIgnoreCase("f")) {

                // Command is "f". Prompt user for search key.
                // Search the database for records whose name begins
                // with the search key. Print these names and the
                // corresponding phone numbers.
                SimpleIO.prompt("Enter name to look up: ");
                String key = SimpleIO.readLine().trim().toLowerCase();
                for (int i = 0; i < numRecords; i++) {
                    String name = records[i].getName().toLowerCase();
                    if (name.startsWith(key)) {
                        System.out.println(records[i].getName() + " " +
                                records[i].getNumber());
                        break;
                    } else if (i == numRecords - 1)
                        System.out.println("Sorry, your search did not" +
                                " match any records.");
                }
            } else if (command.equalsIgnoreCase("d")) {
                SimpleIO.prompt("Enter the name of the record to delete: ");
                String key = SimpleIO.readLine().trim().toLowerCase();
                for (int i = 0; i < numRecords; i++) {
                    String name = records[i].getName().toLowerCase();
                    if (name.startsWith(key)) {
                        records[i] = new PhoneRecord("", "");
                        break;
                    } else if (i == numRecords - 1)
                        System.out.println("Sorry, your search did not match" +
                                " any records.");
                }

            } else if (command.equalsIgnoreCase("q")) {
                // Command is "q".. Terminate program
                System.out.println("You have elected to exit the phone directory.");
                return;

            } else {
                // Command is illegal. Display error message.
                System.out.println("Command was not recognized; " +
                        "please enter only a, f, d or q.");
            }
            System.out.println();
        }
    }
}

// Represents a record containing a name and a phone number
class PhoneRecord {
    private String name;
    private String number;

    // Constructor
    public PhoneRecord(String personName, String phoneNumber) {
        name = personName;
        number = phoneNumber;
    }

    // Returns the name stored in the record
    public String getName() {
        return name;
    }

    // Returns the phone number stored in the record
    public String getNumber() {
        return number;
    }
}

All this being said, when I allocated new array space in this fashion…

else if (numRecords == records.length) {
                    PhoneRecord[] newRecords = new PhoneRecord[records.length*2];
                    for (int i = 0; i < records.length; i++)
                        newRecords[i] = records[i];
                    records = newRecords;
                    SimpleIO.prompt("Enter a new name: ");
                    String name = SimpleIO.readLine().trim();
                    SimpleIO.prompt("Enter new phone number: ");
                    String number = SimpleIO.readLine().trim();
                    newRecords[numRecords] =
                            new PhoneRecord(name, number);
                    numRecords++;

…the program did exactly what I needed it to do, namely, double the size of the array with each user entry and never print the “Sorry, but the database is full” message. My question is, why can’t I get the program to work with the .arraycopy method? Any help would be much appreciated.

When I do this…

else if (numRecords == records.length) {
                    PhoneRecord[] newRecords = new PhoneRecord[records.length*2];
                    System.arraycopy(records, 0, newRecords, 0, records.length);
                    **newRecords = records;**
                    SimpleIO.prompt("Enter a new name: ");
                    String name = SimpleIO.readLine().trim();
                    SimpleIO.prompt("Enter new phone number: ");
                    String number = SimpleIO.readLine().trim();
                    newRecords[numRecords] =
                            new PhoneRecord(name, number);
                    numRecords++;

…is when I get the arrayindexoutofbounds error.

  • 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-20T11:30:21+00:00Added an answer on May 20, 2026 at 11:30 am

    Arrays have a fixed length in Java. If you want a dynamic-length array, rather than programming it from scratch, you should use the one given for you in the standard API : java.util.ArrayList. Look at its documentation here : http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html. This class is part of the Java collections API, which is a must-know for any Java programmer. Learn to use them in the Java tutorial.

    Rather than a PhoneRecord[], you would have a list of PhoneRecords :

    List<PhoneRecord> records = new ArrayList<PhoneRecord>();
    

    You can add a new record with

    records.add(newRecord);
    

    and the array wrapped by the array list will automatically grow if needed.

    And you can access to a specific index with

    PhoneRecord record = records.get(index);
    

    You can also iterate on the list as you would with an array :

    for (PhoneRecord record : records) {
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program here that has an entry box and a button. I
Hi here is my problem. I have a program that calulcates the averages of
Just wondering what little scripts/programs people here have written that helps one with his
I have a program here: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ipc.h> #include
Okay here's the program I have typed up(stdio.h is included also): /* function main
I have something here that is really catching me off guard. I have an
Here's the problem: 1.) We have page here... www.blah.com/mypage.html 2.) That page requests a
Since upgrading to 2008 I and many people here have noticed that randomly VS
I have a project here that connects to an in-production database and grabs tables
I have to create a program that finds all the possible ways of filling

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.