Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: multidimentional array- names & gender.

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default multidimentional array- names & gender.

    Hello Everyone,

    I just started java programming and I 'm having some road block in doing a simple multi-dimensional array. I want to build an array with five names two females and the rest male. I want to be able to print names with their gender in one column numbered 1 thru. 5.
    Any help on this or any examples?

    Thanks,

    Astro-one.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: multidimentional array- names & gender.

    What have you tried?

    Can you post an example of what would be in the columns and rows of the array?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: multidimentional array- names & gender.

    Hello Norm,

    The names would be
    Jack,
    Sally,
    Dave,
    Brian,
    Sue,

    I want the small example program to be able to distinguish the two females and the three males by printing their names, their correct gender example: Dave, Male. I just want to see a working program . I have tried several times and not getting no where with many errors.

    Thanks,
    Astro-one

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: multidimentional array- names & gender.

    That looks like what would go in a one dim array. One column in each row that holds a name.
    Why do you think the code needs a multi-dim array?

    See post#2
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: multidimentional array- names & gender.

    Yes, I have done a simple array where I put in the name with gender however, I want to be able to input the names and have the program search and print out the names with their genders. So in the first column the names and the second column female or male genders.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: multidimentional array- names & gender.

    Can you post a sample of what the columns and rows of the array would contain?

    Also post the code you are having problems with.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: multidimentional array- names & gender.

    Hi Norn,

    This is what I have so far , however, not working.

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

    The result should be:

    Jack Male
    Sally Female
    Dave Male
    Sue Female
    Brian Male

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: multidimentional array- names & gender.

    What is the current output of the program?

    For debugging, this method is useful for formatting two dim arrays:
    System.out.println("an ID "+ java.util.Arrays.deepToString(theArrayName));
    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    When coding loops the go through arrays, the length of the array should come from the array and not be hardcoded as 4 or 2
    use the .length attribute:
    first Dim length is theArray.length
    second dim length: theArray[anIndex].length
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: multidimentional array- names & gender.

    Hi Norm,

    HUMMM!, Ok I will try to correct, understand some of your contents, and give it a try.

    Thanks,

    --- Update ---

    Hi Norm,

    I forgot to mention that I could not get any output other than error message.

    Thanks,

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: multidimentional array- names & gender.

    output other than error message.
    I can't see the error messages from here, so you need to post their full text if you want help with them.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: multidimentional array- names & gender.

    Hi Norm,

    This is the end result that I get with error message.

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

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: multidimentional array- names & gender.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at MultiD.main(MultiD.java:29)
    At line 29 the index to the array is past the end of the array. The array has less than 2 elements in it.
    There is no element at index 1. Remember array indexes range in value from 0 to the array length-1.

    add the statement from post#8 so you can see the contents of the array printed out.

    You should not get that error if you used the .length attribute in the for statement.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. array to store multiple names and data
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: May 6th, 2011, 05:03 AM