Help With arrays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Help With arrays

  1. #1
    Join Date
    Sep 2004
    Posts
    103

    Help With arrays

    I have an assignment where I have to develop a calendar with 12months and I have to have each month with the correct number of days. Then I have to be able to pull out any date in that array and place somethin in there, I am totally confused and have no idea where to start, How Should I go about or at least start this assignment, I have very little experience using arrays and Just need a starting point, Could someone maybe give me an example of one month of an array? This would really help and give me a jump start, Thanks

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Here is some:
    Code:
      public class Months {
      /**
       * mth arrays are zero based.  This could have been
       * fixed by a 'blank' month in arrayPos 0.
       */
      public static int mDay[]={
          31,28,31,30,31,30,31,31,30,31,30,31
      };
      public static String [] mName={
          "jan","feb","mar","apr","may","jun",
          "jul","aug","sep","oct","nov","dec"
      };
      /**
       * Check for leap year
       * @param aYear
       * @return
       */
      public static boolean isLeap (int aYear) {
        if ( (aYear%4) != 0) {
          return false;
        } else if ((aYear%100) == 0 && (aYear%400) != 0) {
          return false;
        } else {
          return true;
        }
      }
      /**
       * Check a date for validity
       * @param year yyyy
       * @param month mm
       * @param date dd
       * @return
       */
      public boolean isOkDate (int year, int month, int date) {
        boolean dateOK=false;
        month -= 1; // Zero based month
        if (month < 0 || month > 11) {
          dateOK = false; // dumb month
        } else if (month != 1) {
          dateOK = (date >=1 && date <= mDay[month]);
        } else { // its february
          int febDays=(isLeap(year)) ? 29: 28;
          dateOK =  (date >=1 && date <= febDays);
        }
        if (dateOK)
          System.out.println("date: "+mName[month]+" "+date+", "+year+" is ok");
        else
          System.out.println("date: "+(month+1)+"."+date+"."+year+" is nonsense");
    
        return dateOK;
      }
    }
    eschew obfuscation

  3. #3
    Join Date
    Sep 2004
    Posts
    103

    thank you sjalle

    okay I took your idea and used it, I don't know if I did the same thing but here is what I have. I have another question. Heres my array of months and dates, How can I allow a user from DOS to select this month? I would like it so that the user types in the number that the months are equal to, pull out a certain date and enter something in that array like I would set a date for that day. Is this possible and can you give me tips on how to do this this, or if Anyone has an answer?

    // here I show the user in DOS what the calendar consists of
    System.out.println("This calendar Consists of 12 months");
    System.out.println("");

    // now here is my array, And the Months are Set to numbers, not right now but Technically that's what I want.

    months = new String[13];
    months[0] = null;
    months[1] = "January " + 31 + " = 1";
    months[2] = "February " + 28 + " = 2";
    months[3] = "March " + 31 + " = 3";
    months[4] = "April " + 30 + " = 4";
    months[5] = "May " + 31 + " = 5";
    months[6] = "June " + 30 + " = 6";
    months[7] = "July " + 31 + " = 7";
    months[8] = "August " + 31 + " = 8";
    months[9] = "September " + 30 + " = 9";
    months[10] = "October " + 31 + " = 10";
    months[11] = "November " + 30 + " = 11";
    months[12] = "December " + 31 + " = 12";
    for (int k = 1; k < months.length; k++) {
    System.out.println(months[k]);
    }

    // When the user enters 1 I want to pull up the month of january, and enter somethin in one of the days between 1 - 31? I want to be able to set dates for every month, Can anyone Help?

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I'm not sure what you mean, you say "I want to be able
    to set dates for every month". In the context you've
    written it it doesn't make any sense to me. Please
    clarify

    BTW have you tried to print out the "months" in your
    array ?
    eschew obfuscation

  5. #5
    Join Date
    Sep 2004
    Posts
    103

    Reply

    When i want to set the dates for each month, I want to pull out January 25, of the array and then Enter, " I have a dentist Appointment", I want to access January 1, Then I enter " I have an appointment at 12", and If I have somethin on January 12 It'll say Cannot enter, Or delete Appointment, This is what I mean by Setting the dates for each month. I can't figure out how to pull out stuff from the array.

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Well, that requires a GUI/dos-shell menu and a permanent data storage, e.g. a file or a database. You
    cannot expect the program to remember just by code.
    eschew obfuscation

  7. #7
    Join Date
    Sep 2004
    Posts
    103

    No gui interface allowed

    okay, I talked with my proffessor today and he doesn't give any hints at all, what an a hole, But i've tried another method, using the gregorian Calendar, I've tried to use the scanner class but it freezes when I run the program can I get any help here?


    import java.util.*;

    public class calendar2{
    public static void main (String [] args){

    Scanner in = new Scanner(System.in);

    GregorianCalendar d = new GregorianCalendar();

    int today = d.get(Calendar.DAY_OF_MONTH);
    int month = d.get(Calendar.MONTH);
    int k = in.nextInt();

    d.set(Calendar.DAY_OF_MONTH,1);

    int weekday = d.get(Calendar.DAY_OF_WEEK);

    System.out.println("Sun Mon Tue Wed Thu Fri Sat");

    for (int i = Calendar.SUNDAY; i < weekday; i ++)
    System.out.print(" ");
    do{
    int day = d.get(Calendar.DAY_OF_MONTH);
    System.out.printf("%3d", day);
    if (day == today)
    System.out.print("*");
    else
    System.out.print(" ");
    if (weekday == Calendar.SATURDAY)
    System.out.println();
    d.add(Calendar.DAY_OF_MONTH,1);
    weekday = d.get(Calendar.DAY_OF_WEEK);
    }
    while (d.get(Calendar.MONTH) == month);
    if (weekday != Calendar.SUNDAY)
    System.out.println();
    System.out.println("Please enter the date you which to select " + k);
    System.out.println("");
    System.out.println("Would you like to set up an Appointment With");
    System.out.println("Dentist Number 1");
    System.out.println("Dentist Number 2");
    System.out.println("Dentist Hygienist 3");
    System.out.println("Dental Hygienist 4");
    System.out.println("Dental Hygienist 5");


    }
    }

  8. #8
    Join Date
    Sep 2004
    Posts
    103
    it locks up at

    int k = in.nextInt();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center