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 7 of 7

Thread: 2D array input

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default 2D array input

    Here is the Lab information


    Create a program that keeps track of the following information input by the user:
    First Name, Last Name, Phone Number, Age

    Now - let's store this in a multidimensional array that will hold 10 of these contacts. So our multidimensional array will need to be 10 rows and 4 columns.
    You should be able to add, sort (by any column you chose) and remove contacts in the array.


    Questions

    I can code all the information provided that there are 1D arrays. But my Professor wants me to do them in 2D which is a little of challenge for me. My question is if my I do a 2D array will that be in conflict with m phone number and Age column?

    --- Update ---

    I was going to start with this.

     for(int row = 0; row < demographic.length; row++)
            {
                for(int column = 0; column < demographic[row].length; column++)   
                {
                  demographic[row][column] = "  ";
                  System.out.print( demographic[row][column] );          
                }
                System.out.println();                                                      
            }


    --- Update ---

    Don't know why i can not edit my post but here is the beginning code.

         Scanner scan = new Scanner(System.in);
            String[][] demographic = new String[10][4];
     
     
     
            for(int row = 0; row < demographic.length; row++)
            {
                for(int column = 0; column < demographic[row].length; column++)   
                {
                  demographic[row][column] = "  ";
                  System.out.print( demographic[row][column] );          
                }
                System.out.println();                                                      
            }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: 2D array input

    I don't understand your question. What do you mean by conflict?

    Another way to look at "multi-dimensional" arrays is to think of them as "arrays of arrays." In this case, the required 1D array looks like:

    String[] contacts = { "FirstName", "LastName", "PhoneNumber", "Age" };

    which is one row of 4 elements or columns. If there are to be multiple contacts, then there must be multiple rows, so that:

    contacts[0] = { "FirstName0", "LastName0", "PhoneNumber0", "Age0" };
    contacts[1] = { "FirstName1", "LastName1", "PhoneNumber1", "Age1" };
    contacts[2] = { "FirstName2", "LastName2", "PhoneNumber2", "Age2" };

    and so on . . .

    Then to specify LastName1, the second element (or column) of the 2nd row:

    contacts[1][1].

    I'm sure you'll still have questions, so come back with more.

  3. #3
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: 2D array input

    GregBrannon,

    Thank You so much for the quick response. What I mean by is Firstname and LastName are String type data. And my phone number and age is int type data. I'm just confuse on how I will declare this array. I was going to use a string type array but notice the last data types where int type. Any suggestions?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: 2D array input

    An array must contain the same data types. Do phone number and age have to be ints? If not, consider making all Strings. If they must be ints, you can convert back and forth from String to int (or Integer), but there can't be different types in the same array. Converting isn't a big deal and is done often as needed.

  5. #5
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: 2D array input

    okay.....This may sounds a little ignorant but I never actually stored numbers as String type variables. Is there something I read or refer to.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: 2D array input

    "(123) 456-7890" is a string of numbers that could be a phone number. "32" is a string of numbers that could be someone's age. It's pretty simple, really. I can't think of a reference to point you to that explains it in greater detail, and I think the words would be wasted.

  7. The Following User Says Thank You to GregBrannon For This Useful Post:

    Rain_Maker (September 13th, 2013)

  8. #7
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: 2D array input

    ohh wow I never that of it like that lol Thanks!

Similar Threads

  1. [SOLVED] Return an array of Pairs that have the same length as the input array of Strings.
    By hemla in forum Object Oriented Programming
    Replies: 3
    Last Post: August 8th, 2013, 08:17 AM
  2. Input into array.
    By Saintroi in forum What's Wrong With My Code?
    Replies: 22
    Last Post: May 18th, 2012, 08:12 AM
  3. Input in a two Dimensional Array.
    By Mr.777 in forum File I/O & Other I/O Streams
    Replies: 29
    Last Post: December 14th, 2011, 11:40 PM
  4. Comparing Input to array
    By hbonh in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 28th, 2011, 10:43 AM
  5. Different operation on Array
    By jempot in forum Collections and Generics
    Replies: 4
    Last Post: January 27th, 2009, 06:07 AM