#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Homework Help = Arranging Strings in a 2D array


    So I have this assignment, it's a bit detailed but part of the assignment requires me to make a calender that holds 8 periods for weekdays. Each period is filled with an activity, a start time and an end time.
    I decided to make a 2D array for each day as I'm going to have to turn this into a .txt file later.
    My goal is to have my array for one day look like this

    Activity 1 - Activity 2 - Activity 3 - Activity 4
    Start Time - Start Time - Start Time - Start Time
    End Time - End Time - End Time - End Time - End Time.....
    And so on until Activity 8.
    I have no problem filling in Activity 1, Start Time 1 and End Time 1. But I'm not sure how to even access the other rows and collums of the array let alone jam a String into them.

    I tried this.....
    Code:
    public void mondays() { monday = new String[3][8]; try { do { System.out.println("Please enter the activity, time it starts and time it ends in that order please"); input2 = br.readLine(); monday[loop][secloop] = input2; loop++; }while(loop <= 3); loop = 0; do { System.out.println("Please enter the next activity, time it starts and time it ends in that order"); input4 = br.readLine(); monday[loop][secloop] = input4; loop++; }while(loop >= 3);
    In hopes that perhaps input3 would be able to enter itself as the next collumn but that was an utter failure.

    Help would be much ppreciated.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Can you change the posted code so it is not all on one line?

    Can you define what goes in the columns for each row?
    What is in the first and the second and third, etc
    And what goes in a row.

    What you posted is confusing.
    Activity 1 - Activity 2 - Activity 3 - Activity 4
    Start Time - Start Time - Start Time - Start Time
    End Time - End Time - End Time - End Time - End Time.....
    I would think the rows would be for each period and the columns for a row would be the activity and times
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0
    Whoops, sorry about the code

    This is what I want it to be.....

    http://i220.photobucket.com/albums/dd91/DPXJube/MMMMGADA.png

    Hold on, I'll try and reprint the code

    Code:
    public void mondays() 
    {
     monday = new String[3][8]; try 
    {
     do
    { 
    System.out.println("Please enter the activity, time it starts and time it ends in that order please"); 
    input2 = br.readLine(); 
    monday[loop][secloop] = input2; 
    loop++; 
    }while(loop <= 3); 
    loop = 0; 
    do 
    { 
    System.out.println("Please enter the next activity, time it starts and time it ends in that order"); 
    input4 = br.readLine();
     monday[loop][secloop] = input4;
     loop++; 
    }while(loop >= 3);
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Can you explain what your problems with the code are? You haven't answered any of the questions I asked in post #2.

    The code posted is not properly formatted to show the logic. Each nested level within a pair of {}s should be indented 3-4 spaces to make it easy to read the code and the logic levels.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0
    I have a 2 dimensional array that I have defined as
    monday = new String[3][8]
    I want to be able have a user input activities in the FIRST row.
    The first row should ONLY have activities.
    The SECOND and THIRD row should only have times(second row being start time and third row being end time)

    In other words, the rows would be for each activity and the columns would be for the time.

    My dilemma here is I have no idea how to access more then one column.
    In my code I can input the information for column 1.
    After that I cant input anymore as it will give me an out of bounds error as it should. The column is filled up and no more information should be put into it. I'm trying to figure out how to access the second column so that I can input information into there and so on until I have reached 8 columns with 3 rows worth of information.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    The array you are defining: String[3][8] is normally treated as having 3 rows with each row having 8 columns.
    rows would be for each activity and the columns would be for the time.
    That says there are three activities and 8 times. Is that right?
    how to access more then one column.
    To access an element on the row=rowIdx and column=columnIdx code this way:
    Code:
    array[rowIdx][columnIdx] //  access an element in array
    To fill the columns in a row, set rowIdx to the row and change the columIdx from 0 to the number of columns-1 as you assign values to the column's elements.
    Then change the rowIdx and do it again.
  12. #7
  13. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,554
    Rep Power
    1621

    A normal array would suffice


    You do not actually need a 2D array for accomplishing your task. Simply declare an array of 5, in which each element in the array corresponds to a weekday of a week.

    However, due to the fact that you need to hold other information such as up to 8 periods of time in a weekday along with the activities taking place at a given period, you should somehow declare your own class with properties to hold these information and the array of 5 should be declared of this class you create.

    Generalising this to hold up to 8 entries of an organiser for a weekday up to one whole year, you may even use an array to hold all entries for each day, where each element in the array corresponds to the collection of entries (represented by an instance of your custom class holding an array of 8). You may declare yet another custom class to serve as an atomic unit comprising 1 activity entry within a weekday. That means, your custom class should hold the collection of 8 instances of activity-time entry.

    Putting this to pseudocodes, it should sound like the following:
    java Code:
     
    /** declare a class to hold activities and time. **/
    public class Appointment{
        private String description;
        private Date time;
    // add more methods for accessing these properties.
    }
     
    /** declare yet another class to hold a collection of your appointments**/
    public class OrganiserEntry{
        private Appointment[] appointments = new Appointment[8];
        //I actually prefer to use ArrayList for handling arbitrary entries
     
        //declare methods to manipulate or access or search for an entry within the collection.
    }
     
    /** declare a custom class to hold 5 entries (1 for each weekday) for a week **/
    public class WeeklyEntry{
         private OrganiserEntry[] weeklyEntries= new OrganiserEntry[5];
        //declare convenience methods to access/search/set entries.
     
    }
     
    /** finally declare your main class holding activity entries for all weekdays **/
    public class Organiser{
        //assuming there are 52 weeks in a year.
        private WeeklyEntry[] entries = new WeeklyEntry[52];
        //declare methods to manipulate/search entries.
    }
    When the programming world turns decent, the real world will turn upside down.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0
    import java.util.Arrays;
    import java.util.Comparator;

    public class Asdf {

    public static void main(final String[] args) {
    final String[][] data = new String[][] {
    new String[] { "2009.07.25 20:24", "Message A" },
    new String[] { "2009.07.25 20:17", "Message G" },
    new String[] { "2009.07.25 20:25", "Message B" },
    new String[] { "2009.07.25 20:30", "Message D" },
    new String[] { "2009.07.25 20:01", "Message F" },
    new String[] { "2009.07.25 21:08", "Message E" },
    new String[] { "2009.07.25 19:54", "Message R" } };

    Arrays.sort(data, new Comparator<String[]>() {
    @Override
    public int compare(final String[] entry1, final String[] entry2) {
    final String time1 = entry1[0];
    final String time2 = entry2[0];
    return time1.compareTo(time2);
    }
    });

    for (final String[] s : data) {
    System.out.println(s[0] + " " + s[1]);
    }
    }

    }
    Last edited by E-Oreo; February 11th, 2013 at 08:35 AM. Reason: removing spam

IMN logo majestic logo threadwatch logo seochat tools logo