954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to used 2 different method to populate and show String array content

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] );
              }
betny
Newbie Poster
20 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

Do you have a question?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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] );

}

}
}

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

betny
Newbie Poster
20 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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)

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: