3 Replies Latest reply: Apr 20, 2013 10:37 AM by rp0428 RSS

    learning java programing - Array issues.

    1004282
      Hi Everyone,

      I just started in java programming and into Arrays multidimensional. I started a simple 2 dimensional array with 5 names with genders. I 'm having issues because it does not want to print out the names with correct genders Male/ Female. The simple program should print out the 5 names with gender type.

      example:

      Jack - Male
      Sally - Female
      Dave - Male
      Sue - Female
      Brian - Male

      Here is what I came up with so far:

      public class Multiname
      {
           public static void main(String[] args)
           {
                //String[][] multiname;
                String [][] multiname =
                {
                          {"Jack", "sally"},
                          {"Dave", "Brian"},
                          {"Sue"}
           };
                /* multiname [0][0] = "Jack";
                multiname [0][1] = "Male";
                multiname [1][0] = "Sally";
                multiname [1][1] = "Female";
                multiname [2][0] = "Dave";
                multiname [2][1] = "Male";
                multiname [3][0] = "Sue";
                multiname [3][1] = "Female";
                multiname [4][0] = "Brain";
                multiname [4][1] = "Male";     
                */                                   
                //int rows = 2;
           //int columns = 2;
                     for(int i = 0; i < 4; i++)
                          {for(int j = 0; j < 2; j++)
                             {
                                  System.out.println("multiname " + ": " + multiname [i][j]);               
                          }
                          }
           }
      }

      This is the result:
      multiname : Jack
      multiname : sally
      multiname : Dave
      multiname : Brian
      multiname : Sue
      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
           at MultiD.main(MultiD.java:29)

      Can someone inform me what is wrong or missing, and what do I need to do to correct this. I'm new to this and I'm a visual person. Any assistance would be appreciated.

      Thanks,
        • 1. Re: learning java programing - Array issues.
          rp0428
          Welcome to the forum!

          When you post code use \
           on the line before and on the line after the code to preserve formatting (read the FAQ). You can use the 'Preview' tab on the reply window to see what your reply will look like.
          
          Please edit your post and add the code tags.
          {quote}
          The simple program should print out the 5 names with gender 
          {quote}
          How could it possibly do that when you commented out the lines the put the gender into the arrays?
          /* multiname [0][0] = "Jack";
          multiname [0][1] = "Male";
          multiname [1][0] = "Sally";
          multiname [1][1] = "Female";
          multiname [2][0] = "Dave";
          multiname [2][1] = "Male";
          multiname [3][0] = "Sue";
          multiname [3][1] = "Female";
          multiname [4][0] = "Brain";
          multiname [4][1] = "Male";
          */
          When you are starting out don't use any more data than you need to get things working. You only need two name/gender combinations; any more just gets in the way.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
          • 2. Re: learning java programing - Array issues.
            1004282
            Hello,

            You'll have to excuse me for I'm new to this programming. I will read the FAQ. I read your response and I appreciate this however; I'm not understanding your answer. can you provide a visual on this ?

            Thanks,
            Astro1
            • 3. Re: learning java programing - Array issues.
              rp0428
              I DID provide a visual in my last reply when I said this
              >
              How could it possibly do that when you commented out the lines the put the gender into the arrays?