private String [][] personalDetails() {

        logger.debug( "personalDetails()" );

        String personArray [][] =
            {
              { "John",  " Muray", "1-10-1982" },
              { "Peter",  " Kamau", "2-5-1987" },
              { "Mary",  " Wambui", "8-5-1988" },
              { "Bethu",  " Kips", "8-8-1987" },
              { "Cetera",  " Omosh", "2-5-1987" }

            };   
           
          return personArray;

       
    }
  
       /**
     * Demonstrates initializing and populating a multi-dimensional array
     * in one statement
     */
   private void personalDetailsContent( ) {

        logger.debug( "personalDetailsContent()" );
            
             String [][] personArray= new String[ 5 ][5];
              for(int i=0; i<=personArray.length; i++){
                  for(int j=0; j<=personArray[i].length; j++){
                   personArray[i][j]=i;
                  }
                System.out.println( "Names " + i +  "" + j +" : " + personArray[ i ][j] );
              }

Recommended Answers

All 3 Replies

Do you have a question?

    Please create a multi dimensional array that stores the following
    information for 5 people:

        * First Name
        * Last Name
        * Date of Birth

    Your application should have one method that populates the multi
    dimensional array with data and another method that shows the contents
    of the multi dimensional array.

Have gone this far:

 private String [][] personalDetails() {

    logger.debug( "personalDetails()" );

    String personArray [][] =
        {
          { "John",  " Muray", "1-10-1982" },
          { "Peter",  " Kamau", "2-5-1987" },
          { "Mary",  " Wambui", "8-5-1988" },
          { "Bethu",  " Kips", "8-8-1987" },
          { "Cetera",  " Omosh", "2-5-1987" }

        };   

      return personArray;


    }

   /**
 * Demonstrates Showing the content a multi-dimensional array
 * in a different method from populating method.
 */

   private void personalDetailsContent( ) {

    logger.debug( "personalDetailsContent()" );

         String [][] personArray= new String[ 5 ][5];

          for(int i=0; i<=personArray.length; i++){

              for(int j=0; j<=personArray[i].length; j++){
                System.out.println( "Person index " + j +" : " + personArray[i][j] );

              }

          }
}

Errror:

Error: Exception in thread 'main' java.lang.IndexOutOfBoundException: 5

Arrays are 0 based,, so a [5] array has length 5 and indexes 0-4
Your loop
for(int i=0; i<=personArray.length; i++){
will attempt to access indexes 0-5, and will fail on the last one with an IndexOutOfBounds (5)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.