-2

I'm trying to write code that starts with day 1 (and whatever weekday that happens to be) and print out consecutively. For example, if Friday is 1, it should next print Saturday 2, then continue counting the days as normal but reset the dayOfTheWeek to Sunday.

My current code is:

String[] dayOfTheWeek ={“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”};
int[] days ={31}
for(int days=1; days<32; days=days++){
    for(String dayOfTheWeek=5; dayOfTheWeek<7;  dayOfTheWeek=dayOfTheWeek++){
         System.out.println(dayOfTheWeek" " days)
    }
}

It doesn't work at all, and I don't have the experience to know where to start. If someone could point me in the right direction that would be greatly appreciated.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 3
    Why not? What specific part of implementing this are you having difficulty with? – Mike 'Pomax' Kamermans Mar 26 '21 at 04:00
  • 1
    Please read [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822). If you are so lost that you don't even know how to start designing and coding the algorithm, I suggest that you talk to your teachers. – Stephen C Mar 26 '21 at 04:02
  • 2
    [Open letter to students with homework problems](https://softwareengineering.meta.stackexchange.com/q/6166/202153) – Andreas Mar 26 '21 at 04:17
  • @Mike'Pomax'Kamermans, I suppose a good place to start is using 2 arrays together, then I could probably figure out the rest on my own, lets use January as an example, how would I go about printing January 1, January 2, January 3 and so forth using the arrays together? the only way I can think to try is manually printing each number. – Mental Overload Mar 26 '21 at 04:26
  • Why? You have an array with the number of days in each month right there, called `daysInMonth`. So you use a for loop starting at `int day =1` and ending after printing the `day` that is equal to the number of days in that month. For January (which is `months[0]`) the number of days in the month is 31 (found in `daysInMonth[0]`). – Mike 'Pomax' Kamermans Mar 26 '21 at 04:28

3 Answers3

2

This is a fine exercise. Just for the bigger picture, it’s nothing one would use for production code in real life.

There are basically two ways. Both will work fine.

  1. Use two nested loops. The outer loop will iterate over the months of the year. The inner loop will iterate over the days of the month.
  2. Use one loop over the days of the year.

In both cases you will also need to keep track of the day of the week throughout. Edit again: If you know about enum in Java, use an enum for the months and another enum for the days of the week. If you don’t know enums, as Andreas has already mentioned, use int for each of month, day of month and day of week. Use the ints for looking up the strings in your arrays. Because an int can be incremented and because array indices are ints.

Further edit: To answer the question in your title, assuming that you are using an int index into dayOfTheWeek to represent the next day of the week. There are several ways to increment for each day printed. I find this way natural and simple:

    dayOfWeekIdx++;
    if (dayOfWeekIdx == dayOfTheWeek.length) { // Overflown
        dayOfTheWeek = 0; // Reset
    }

Some prefer this much shorter way:

    dayOfTheWeek = (dayOfTheWeek + 1) % dayOfTheWeek.length;

The modulo operator, % gives you the remainder after dividing. So when dayOfTheWeek overflows, that is, becomes 7, the division is 7 divided by 7, which is 1 with a remainder of 0, so dayOfTheWeek gets reset to 0.

In the version with one loop over all the days of the years you may do similarly to four-line solution for day of week, only inside the if statement add a line for incrementing the month index.

So what would one use for production code? This is mostly for other readers. One would use LocalDate and also DateTimeFormatter, both from java.time, the modern Java date and time API. No one in their right mind would use Date nor Calendar, the classes that you mentioned in the question, since they are both poorly designed and long outdated.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I have done something similar with ints for all my loops, but I'm struggling with using strings – Mental Overload Mar 26 '21 at 05:59
  • @MentalOverload You need to use `int`. And then use `int` for looking up the string to be printed in your array. This goes for both month and day of week. – Ole V.V. Mar 26 '21 at 06:05
  • it will take me a while to figure this out all the way, but you have been immensely helpful – Mental Overload Mar 26 '21 at 06:32
  • It’s good to see that you’re fighting it, As I said, it’s a good exercise, so you are learning at the same time. I have edited again to explain the use of `int` a bit more. – Ole V.V. Mar 26 '21 at 15:17
1

Start with 3 variables:

int dayOfWeekIdx = 5/*Friday*/;
int monthIdx = 0/*January*/;
int day = 1;

Now print the first date, using the two xxxIdx variables as indexes into the daysOfTheWeek and months arrays.

Increment both dayOfWeekIdx and day. Check them for overflow and reset to 0 as needed.

If day overflowed, increment monthIdx. Exit if it overflowed.

Loop back to print the next date.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

I am trying to write a code that starts with day 1 (and whatever weekday that happens to be) and print out consecutively for example if Friday is 1, it should next print Saturday 2, the continue counting the days as normal but reset the dayOfTheWeek to Sunday.

Use the % operator which gives you the value of the remainder. Also, use List<String> instead of String[] to make it easier for you to process the input. List has a rich API to process an input w.r.t. the elements it contains.

import java.util.List;

public class Main {
    public static void main(String[] args) {
        // Test
        printDaysStartingWith("Tuesday");
    }

    static void printDaysStartingWith(String dayName) {
        List<String> dayOfTheWeek = List.of("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
                "Saturday");
        int index = dayOfTheWeek.indexOf(dayName);
        if (index != -1) {
            for (int day = index; day < 31; day++) {
                System.out.println(dayOfTheWeek.get(day % 7));
            }
        } else {
            System.out.println("Error: Wrong parameter.");
        }
    }
}

Output:

Tuesday
Wednesday
Thursday
...
...
...
Monday
Tuesday
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110