The application should have one method that populates the multidimensional array with data and another method that shows the contents
of the multi dimensional array. Both methods should be triggered from the main method. Assist because am been notified that the code below is not correct

public class ArrayExample {



    // Getting logger used for logging statements.
    public Logger logger =
        Logger.getLogger( ArrayExample.class );


    /**
     * Constructor
     */
    public ArrayExample() {
        super();
    }

     /**
     * Demonstates initializing and populating a multi-dimensional array in
     * several statements
     * multi dimensional array that stores 
     * information for 5 people:
     * First Name
     * Last Name
     * Date of Birth
     */

      private String [][] personalDetails() {

        logger.debug( "personalDetails()" );

        String personArray[][]= {

        { "John","Kamau", "30 march 1984" },
        { "Kevin","Kubai", "30 March 1988" },
        { "Suzzie","Gichia","23 December 1992" },
        { "Wayne","Rooney","1 August 1986" },
        { "Bob","Marley", "16 February 1945" }
        };

    return personArray;
}   

     /**Shows the contents of the multidimesional Array
       * 
       */
       private void personalDetailsContent( ) {

      logger.debug( "personalDetailsContent()" );

        String [][] personArray= personalDetails();

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

         for(int x=0; x<personArray[i].length; x++){
         System.out.print(personArray[i][x]+"\t");

       }
        System.out.println();
       }

    }

    /**
     * This is the starting execution point of the program.
     *
     * @param args the args are parameters passed into this java program from
     * the command line. 
     */
    public static void main( String[] args ) {

        // Configure the logger to print to the screen
        BasicConfigurator.configure();


        ArrayExample arrayExample = new ArrayExample();
        arrayExample.personalDetails();
        arrayExample.personalDetailsContent( );

    }
}

Recommended Answers

All 10 Replies

you may want to elaborate a bit more on the actual assignment.
one thing though, you may want to keep your two-dimensional array as a variable in another scope, either on class scope, or in the main method itself.

the method that should show the contents shouldn't re-populate the array, but it should take it as a parameter.

Should one method that populates the multidimensional array with data and another method that shows the contents
of the multi dimensional array. Both methods should be triggered from the main method.

what am i doing wrong??

try a construction like this:

public static void main(String[] args){
  String[][] data = populateArray();
  showArrayContents(data);
}

I have tried it and it has an error ...Non static method personDetails().. cannot be referenced from a static context..

And the second error ... personDetailContents()
cannot be applied to(java.lang.string)

I have tried it and it has an error , Thankiossk Cool!

well, you will need to adapt the rest of your code accordingly. how much experience do you have in java development?

Not much.. am in the learning process..

create an array as instance variable, that way all the methods in that class have access to it. and perform your updates (adding/removing/...) using methods in that class.

Is it correct

public class ArrayExample {



    // Getting logger used for logging statements.
    public Logger logger =
        Logger.getLogger( ArrayExample.class );


    String personArray[][]= {

        { "John","Kamau", "30 march 1984" },
        { "Kevin","Kubai", "30 March 1988" },
        { "Suzzie","Gichia","23 December 1992" },
        { "Wayne","Rooney","1 August 1986" },
        { "Bob","Marley", "16 February 1945" }
        };

    /**
     * Constructor
     */
    public ArrayExample() {
        super();
    }

     /**
       * Date of Birth
       */

      private String[][] personalDetails() {

        logger.debug( "personalDetails()" );
         return personArray;
}   

     /**Shows the contents of the multidimesional Array
       * 
       */
       private void personalDetailsContent( ) {

      logger.debug( "personalDetailsContent()" );

        String [][] personArray= personalDetails();

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

         for(int x=0; x<personArray[i].length; x++){
         System.out.print(personArray[i][x]+"\t");

       }
        System.out.println();
       }

    }

    /**
     * This is the starting execution point of the program.
     *
     * @param args the args are parameters passed into this java program from
     * the command line. 
     */

    public static void main( String[] args ) {



        // Configure the logger to print to the screen
        BasicConfigurator.configure();


        ArrayExample arrayExample = new ArrayExample();
        arrayExample.personalDetails();
        arrayExample.personalDetailsContent( );

    }
}

it's inefficient, you're calling methods to get values you already have, not to mention you've declared a getter as private. unless only the owning class should have access (which isn't the purpose here) don't do that. I'll give you an example:

import javax.swing.JOptionPane;

public class ArrayExample {
    String[][] personArray;

    /**
     * The default constructor, in which you set the default size 
     */
      public ArrayExample(){
          personArray = new String[5][3];
      }
      /**
       * A second constructor, which allows you to set the size of the number of persons
       * @param numberPersons
       *    The number of persons (depth of your first dimension array)
       * @throws Exception 
       *    This exception is thrown if you specifiy either a negative number or 0.
       *    Normally, I would create a new type of exception, but for this example, the default Exception class will do.
       */
      public ArrayExample(int numberPersons)
        throws Exception{
          if( numberPersons < 1 )
              throw new Exception("You need to have at least one element");
          personArray = new String[numberPersons][3];
      }
      /**
       * The getter which returns the current personArray.
       * it's bad OO to make this private. the variable itself should be private, the getters and setters are supposed 
       * to help you to manipulate the values of these private variables.
       * they are used to make sure you can put some validation on them, which you wouldn't have if you gave the using classes
       * direct access to the variable, for an example: check the setter method.
       * @return 
       *    the current personArray
       */
      public String[][] getPersonalDetails() {
         return personArray;
      }
      /**
       * a setter method without validation, that sets 
       */
      public void setArrayPersons(){
          String[] person;
          for ( int i = 0; i < personArray.length; i++ ){
              person = new String[3];
              person[0] = JOptionPane.showInputDialog("Enter the persons first name");
              person[1] = JOptionPane.showInputDialog("Enter the persons last name");
              person[2] = JOptionPane.showInputDialog("Enter the persons birthdate");
              personArray[i] = person;
          }
      }
      /**
       * a setter method, that takes a two-dimensional array of Strings and replaces the current array with them.
       * the exceptions here aren't necessary, it would work anyway, but, just to give an example of possible validation that wouldn't
       * be possible by direct accessing the variable. let's say we don't want the size of arrays to be able to changed, if we had direct
       * access from outside, we would immediately overwrite the array. in this case, since the validation done, 
       * we will only overwrite the array if both the size of the second dimensional array match.
       * In this example, that comes in handy, since the printElement method only uses the first three elements.
       * if we would replace it with a bigger array, those values would be ignored, if we would replace it by a smaller array
       * we would land an ArrayIndexOutOfBoundsException on running the printElement method.
       * @param newArray
       *    The new Array
       * @throws Exception
       *    is thrown if the size of the inner array (second dimension) doesn't match that of the original.
       */
      public void setArrayPersons(String[][] newArray)
        throws Exception{          
          if ( newArray[0].length != personArray[0].length)
              throw new Exception("sizes of the inner arrays don't match");
          personArray = newArray;            
      }
      /**
       * This method returns a printable String containing the info about one person
       * @param person
       *    a single dimension array, containing the info (first name, lastname, birthdate) from one person
       * @return 
       *    the printable String
       */
      private String printElement(String[] person){
          return person[1] + ", " + person[0] + "\t" + person[2];
      }
      /**
       * This method uses the printElement method to print the contents of the array
       */
      public void showArrayContent(){
          System.out.println("***************************************");
          System.out.println("****  Contents of the personArray  ****");
          System.out.println("***************************************");
          for ( String[] person : personArray )
              System.out.println(printElement(person));
          System.out.println();
      }

    public static void main( String[] args ) {
        ArrayExample arrayExample = new ArrayExample();
        arrayExample.setArrayPersons();
        arrayExample.showArrayContent();
    }
}
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.