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

  • SEARCH
  • Home
  • 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 7639783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:30:34+00:00 2026-05-31T08:30:34+00:00

Here is an error message that keeps coming up as I try to disply

  • 0

Here is an error message that keeps coming up as I try to disply results on my program.

  Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
   at AddressBookIO.getEntriesString(AddressBookIO.java:38)
   at AddressBookEntryApp.main(AddressBookEntryApp.java:42)

I am almost certain my code is correct. My program does everything it is suppose to do except display my results. The “”main” java.lang.ArrayIndexOutOfBoundsException: 0″ is the part that confuses me.

Heres the code for AddressBookIO.java, and AddressBookEntryApp

import java.io.*;

public class AddressBookIO
{
private static File addressBookFile = new File("address_book.txt");
private static final String FIELD_SEP = "\t";
private static final int COL_WIDTH = 20;

// use this method to return a string that displays
// all entries in the address_book.txt file
public static String getEntriesString()
{
    BufferedReader in = null;
    try
    {
        checkFile();

        in = new BufferedReader(
             new FileReader(addressBookFile));

        // define the string and set a header
        String entriesString = "";
        entriesString = padWithSpaces("Name", COL_WIDTH)
            + padWithSpaces("Email", COL_WIDTH)
            + padWithSpaces("Phone", COL_WIDTH)
            + "\n";

        entriesString += padWithSpaces("------------------", COL_WIDTH)
            + padWithSpaces("------------------", COL_WIDTH)
            + padWithSpaces("------------------", COL_WIDTH)
            + "\n";

        // append each line in the file to the entriesString
        String line = in.readLine();
        while(line != null)
        {
            String[] columns = line.split(FIELD_SEP);
            String name = columns[0];
            String emailAddress = columns[1];
            String phoneNumber = columns[2];

            entriesString +=
                padWithSpaces(name, COL_WIDTH) +
                padWithSpaces(emailAddress, COL_WIDTH) +
                padWithSpaces(phoneNumber, COL_WIDTH) +
                "\n";

            line = in.readLine();
        }
        return entriesString;
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
        return null;
    }
    finally
    {
        close(in);
    }
}

// use this method to append an address book entry
// to the end of the address_book.txt file
public static boolean saveEntry(AddressBookEntry entry)
{
    PrintWriter out = null;
    try
    {
        checkFile();

        // open output stream for appending
        out = new PrintWriter(
              new BufferedWriter(
              new FileWriter(addressBookFile, true)));

        // write all entry to the end of the file
        out.print(entry.getName() + FIELD_SEP);
        out.print(entry.getEmailAddress() + FIELD_SEP);
        out.print(entry.getPhoneNumber() + FIELD_SEP);
        out.println();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
        return false;
    }
    finally
    {
        close(out);
    }
    return true;
}

// a private method that creates a blank file if the file doesn't already exist
private static void checkFile() throws IOException
{
    // if the file doesn't exist, create it
    if (!addressBookFile.exists())
        addressBookFile.createNewFile();
}

// a private method that closes the I/O stream
private static void close(Closeable stream)
{
    try
    {
        if (stream != null)
            stream.close();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    }
}

   // a private method that is used to set the width of a column
   private static String padWithSpaces(String s, int length)
{
    if (s.length() < length)
    {
        StringBuilder sb = new StringBuilder(s);
        while(sb.length() < length)
        {
            sb.append(" ");
        }
        return sb.toString();
    }
    else
    {
        return s.substring(0, length);
    }
  }
}

And

import java.util.Scanner;

public class AddressBookEntryApp
{
public static void main(String args[])
{
    // display a welcome message
    System.out.println("Welcome to the Address Book application");
    System.out.println();


    Scanner sc = new Scanner(System.in);


    int menuNumber = 0;
    while (menuNumber != 3)
    {
        // display menu
        System.out.println("1 - List entries");
        System.out.println("2 - Add entry");
        System.out.println("3 - Exit\n");

        // get input from user
        menuNumber = Validator.getIntWithinRange(sc, "Enter menu number: ", 0, 4);
        System.out.println();

        switch (menuNumber)
        {
            case 1:
            {
                String entriesString = AddressBookIO.getEntriesString();
                System.out.println(entriesString);
                break;
            }
            case 2:
            {
                // get data from user
                String name = Validator.getRequiredString(sc, "Enter name: ");
                String emailAddress = Validator.getRequiredString(sc, "Enter email address: ");
                String phoneNumber = Validator.getRequiredString(sc, "Enter phone number: ");

                // create AddressBookEntry object and fill with data
                AddressBookEntry entry = new AddressBookEntry();
                entry.setName(name);
                entry.setEmailAddress(emailAddress);
                entry.setPhoneNumber(phoneNumber);

                AddressBookIO.saveEntry(entry);

                System.out.println();
                System.out.println("This entry has been saved.\n");

                break;
            }
            case 3:
            {
                System.out.println("Goodbye.\n");
                break;
            }
        }
    }
}
}
  • 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-31T08:30:36+00:00Added an answer on May 31, 2026 at 8:30 am

    ArrayIndexOutOfBoundsException is thrown when you try to access an Index, which is more than the size of the specified array.

    Try to change this :

    while(line != null)
    {
        String[] columns = line.split(FIELD_SEP);
        String name = columns[0];
        String emailAddress = columns[1];
        String phoneNumber = columns[2];
    
        entriesString += padWithSpaces(name, COL_WIDTH) +
                           padWithSpaces(emailAddress, COL_WIDTH) +
                           padWithSpaces(phoneNumber, COL_WIDTH) +
                           "\n";
    
        line = in.readLine();
    }
    

    to this :

    while(line != null)
    {
        String[] columns = line.split(FIELD_SEP);
        if (columns.length > 2)
        {
            String name = columns[0];
            String emailAddress = columns[1];
            String phoneNumber = columns[2];
    
            entriesString += padWithSpaces(name, COL_WIDTH) +
                               padWithSpaces(emailAddress, COL_WIDTH) +
                               padWithSpaces(phoneNumber, COL_WIDTH) +
                               "\n";
        }
        line = in.readLine();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gwt project that uses gwt-mosaic. Here is the error message I
Here is the error: System.TypeInitializationException: The type initializer for 'NHibernate.Cfg.Environment' threw an exception. --->
I am trying to update the error message for a CustomValidator that uses a
I have a search bar for my website that keeps sending weird error messages
Here's my code - I am getting an error in my firebug console that
An odd error here, perhaps someone can help track down source as it's attempting
See the full error here: http://notesapp.heroku.com/ I'm using DataMapper and dm-validations 0.10.2. No matter
CREATE TABLE #Report( Cell int, CellValue double) Error here DECLARE @Report TABLE ( Cell
i am developing parser using bison...in my grammar i am getting this error Here
I suck at php, and cant find the error here. The script gets 2

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.