Results 1 to 9 of 9

Thread: Java Array

  1. #1
    lary is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Question Java Array

    Hi. Please i need help with the question below:
    a) A date structure has a day/month format. A day ranges from 1 to 30 and a
    month is one of the months of the year. Specify an array of integers called “day” to hold the values of day, and an array of String called “month” to hold the possible values for a month e.g. January, February, etc. Assume all months have 30 days.
    b) Create and print out on the screen a random date such as 24/December. Note the “/” between the day and the month.
    c) Declare an array called “year” to hold the 360 dates in a year. Initialise a year with the 360 dates, where a date is of the day/month format.

    So far I have done a and b but don't understand c. Would really appreciate help. Here is my code

    Java Code:
    public class Test {
    
        public static void main(String args[])
        {
            int[] day = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,20,30};
            String[] month={"January","February","March","April","May","June","July","August","September","October","November","December"};
    
            System.out.println("Date: "+day[6]+"/" +month[5]);
    
            //String[][] year = new String[day][month]; lost it here.
        }
    
    
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    First, for the random date, you may want to consider using Random, which will make it a randomly chosen index of each array.

    Lets try making the year array a single dimension array which holds strings. Each string in it will hold info like "1/January"

    here is some pseudocode I hope can help you out.
    Java Code:
    loop
      loop
        declare string builder
        add date to string builder
        add / to string builder
        add month to string builder
        add string builder to string to array
      end loop
    end loop
    Last edited by sunde887; 05-08-2011 at 05:16 PM.

  3. #3
    lary is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Question

    Thanks alot. But as you might know i am really new to Java so i really don't know how to apply the pseudocode. Would really appreciate it if you could be more specific.

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by lary View Post
    Thanks alot. But as you might know i am really new to Java so i really don't know how to apply the pseudocode. Would really appreciate it if you could be more specific.
    Java Code:
    import java.util.Random;
    Random r = new Random();
    int index = r.nextInt(10);
    The code above gets a random number from 0-10. Use this to get random indexes.

  5. #5
    lary is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Question

    Thanks alot. I have the code below which gets random days and month but please how do i fill the days array using nested loops?
    Here is my code
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package AuctionPackage;
    
    /**
     *
     * @author Lara
     */
    import java.util.Random;
    public class Test {
    private static class Date {
            public int day;
            public String month;
        }
    
        public static void main(String[] args)
        {
            int[] day = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,20,30};
            String[] month={"January","February","March","April","May","June","July","August","September","October","November","December"};
            Random rand = new Random();
            Date someDate = new Date();
            someDate.day = day[rand.nextInt(day.length)];
            someDate.month = month[rand.nextInt(month.length)];
            System.out.println("Date: "+someDate.day+"/" +someDate.month);
    
           // Date[] days = new Date[360];
            // nested loop to fill days array.
        }
    }

  6. #6
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    I don't really understand what you're asking.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Think about how you would recite the days of your 360 year: "1/January, 2/January, 3/January, ..., 30/January, 1/February, 2/February, ..."

    The pattern is quite simple we have a "fast" loop that goes 1...30 over and over again. And a "slow" loop: Jan...Dec. The fast loop starts a new cycle every time the slow loop moves to the next month.

    This is a very common pattern (for instance number names do the same thing with slower and slower cycles all going through the digits 0...9). Programmers use nested loops to do this. The fast loop is called the "inner" loop and the slow one the "outer" loop for reasons that this code should make clear:

    Java Code:
    public class NestedLoopEg {
        public static void main(String[] args) {
            for(int i = 0; i < 10; i++) {
                for(int j = 0; j < 10; j++) {
                    int product = i * j;
                    System.out.println(i + " times " + j + " is " + product);
                }
            }
        }
    }

    Use a pair of nested loops like this. Instead of calculating a product you would make create the string and assign it to your array.

  8. #8
    lary is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Thanks for your help. Your explanation really helped me. so far i have the code below:
    Java Code:
    import java.util.Random;
    import java.util.ArrayList;
    import java.util.*;
    
    
    public class Test {
    private static class Date {
            public int day;
            public String month;
        }
    
        public static void main(String[] args)
        {
            int[] day={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,20,30};
            String[] month={"January","February","March","April","May","June","July","August","September","October","November","December"};
            Random rand = new Random();
            Date someDate = new Date();
            someDate.day = day[rand.nextInt(day.length)];
            someDate.month = month[rand.nextInt(month.length)];
            System.out.println("Date: "+someDate.day+"/" +someDate.month);
            Date[] year = new Date[360];
    
    
           
           
           for(int i=0; i<month.length; i++)
           {
               for(int j=0; j<day.length; j++)
               {
                  String date = day[j]+"/"+month[i]+"/2011";
                  Arrays.fill(year, date);
                   System.out.println(date);
                   //ArrayList year = new ArrayList();
                   //year.add(date);
                  System.out.println(year);
    
                              }
           }
               }
    }
    I am trying to fill the year array with the 360dates but i get this error.
    Exception in thread "main" java.lang.ArrayStoreException: java.lang.String
    at java.util.Arrays.fill(Arrays.java:2710)
    at java.util.Arrays.fill(Arrays.java:2685)
    at AuctionPackage.Test.main(Test.java:42)


    I am so confused right now and would really appreciate your help. Thanks

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Are you sure you are supposed to use the Date class?

    I ask because your original instructions said "c) Declare an array called “year” to hold the 360 dates in a year. Initialise a year with the 360 dates, where a date is of the day/month format." I might be wrong, but I took that to mean that year was an array of strings.

    The Java Date class represents an instant of time and it does not have a format.

    Assuming an arrray of strings I would have expected something like

    Java Code:
    String[] year = new String[360];
    
    for(int i=0; i<month.length; i++)
    {
        for(int j=0; j<day.length; j++)
        {
            String date = day[j]+"/"+month[i]+"/2011";
            [color=blue]//Arrays.fill(year, date);
            year[/*some index*/] = date;[/color]
            System.out.println(date);

Similar Threads

  1. `java array
    By shane123 in forum New To Java
    Replies: 18
    Last Post: 03-22-2010, 01:42 AM
  2. Java Array Help
    By md69holla in forum New To Java
    Replies: 14
    Last Post: 02-15-2010, 07:02 PM
  3. How to transfer 1D array in JAVA to 3D array in C
    By fishwater00 in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 07:24 PM
  4. how to convert a Java array to a java stack?
    By pompeez in forum New To Java
    Replies: 2
    Last Post: 08-13-2007, 03:41 PM
  5. Help with array in java
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •