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

Thread: Java Array

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Java Array

    Hi. Please i need help urgently with the question below:


    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.
    a) Create and print out on the screen a random date such as 24/December. Note the “/” between the day and the month.
    b) 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 answered "a" but kind of confused about "b". I have used a nested for loop to select the 360 days but don't know how to populate the year array with the date. Here is my code so far.



    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.


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

    Default Re: Java Array

    Have you looked up the text for the error? java.lang.ArrayStoreException
    There is an explanation there for why the error occurs.
    In the statement: Arrays.fill(year, date);
    what is the type of year and what is the type of date? They should be the same.

Similar Threads

  1. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java SE API Tutorials
    Replies: 2
    Last Post: May 17th, 2014, 01:16 AM
  2. RSA Decryption with Java.security - Hex to dec to byte array conversion...
    By SeanSeanston in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 15th, 2010, 09:48 AM
  3. Replies: 4
    Last Post: November 14th, 2010, 11:44 AM
  4. Help needed on java array
    By rossfally in forum Collections and Generics
    Replies: 2
    Last Post: March 4th, 2010, 08:49 PM
  5. Java Webservice problem (Array)
    By Gadge in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 10th, 2010, 11:06 AM