+ 1

Creating Calendar With Alphabets or Letters That Carry-over

Hi, I have a calendar application code attached. My intention is to add alphabets or letters or characters next to the date, which I have done. When you count alphabets from A to Z there are 26. So I'm adding the alphabets beginning from the 1st of each month and when the alphabets reach "Z" they start over again from "A". What I want which I'm failing to do is I want the characters to carry-over to the next month on the calendar. For example ...23|W 24|X 25|Y 26|Z 27|A 28|B 29|C 30|D 31|E 1|F 2|G 3|H... How can I achieve that in my calendar application? https://code.sololearn.com/cKOFu1wsxeLP/?ref=app

6th Feb 2023, 9:18 AM
Sibusiso Mbambo
12 odpowiedzi
+ 2
Would it be that you started from 0 so 0 - 1 % 26 == -1 ? For your original question, your count variable needs to persist across the processes for different months. Try declaring it where you declare "model", "label", and "cal".
7th Feb 2023, 12:15 PM
Lochard
Lochard - avatar
+ 3
It is working? Fact is that I cannot run your code and do not know how those classes work. I'd have guessed that the for loop with count-- should be: for (int day = numberOfDays; day >= 1; day--) with count minus the days of the printed month before going into the previous month.
8th Feb 2023, 4:55 PM
Lochard
Lochard - avatar
+ 2
If you didn't change it, you have: if(count == 26) { count = 0; } count++; You should reverse the order and increment before checking. Or simplify it to count = (count + 1) % 26;
7th Feb 2023, 2:04 PM
Lochard
Lochard - avatar
+ 2
I think you need to calculate the difference between the starting day of the month to print and the ending day of the currently printed month. For just the month before, minus the number of days of the currently printed month and the days of the previous month that you are going to print. Make sure your count won't go negative. Your are welcome.
8th Feb 2023, 2:18 AM
Lochard
Lochard - avatar
+ 2
this will work only on systems with default locales set to calendar similar to US, in some other (e.g.. UK) they get ArrayIndexOutOfBoundsException but you can force US by: Calendar cal = new GregorianCalendar( Locale.US);
21st Feb 2023, 9:35 PM
zemiak
+ 1
Lochard I tried your solution and used a 'count' variable as a Class variable instead of being a local variable. I slightly edited the code like this: for(int day =1; day <= numberOfDays; day++) { model.setValueAt("" + day + alphabet[count % 26], i/7, i%7); i = i + 1; count++; } } as per Mirielle earlier suggestion and it worked as per my need. I only have a slight problem. When I click the 'previous month' button to check the previous month(s) the alphabets pattern is mixed up, but I will try to figure it out and come back when I get stuck. THANK YOU GUYS A MILLION!
7th Feb 2023, 9:51 PM
Sibusiso Mbambo
+ 1
This is what I tried to maintain the pattern of the alphabets. I created a boolean variable 'boolean prevButton = false'. Inside a 'prev' button listener I declared the boolean as 'true' so whenever a previous button is clicked alphabets are reversed based on the current state of alphabets in a calendar. if(prevButton == true) { for(int day =1; day <= numberOfDays; day++) { model.setValueAt("" + day + alphabet[count % 26], i/7, i%7); i = i + 1; } count--; prevButton = false; } else{ for(int day =1; day <= numberOfDays; day++) { model.setValueAt("" + day + alphabet[count % 26], i/7, i%7); i = i + 1; } count++; }
8th Feb 2023, 4:06 PM
Sibusiso Mbambo
0
I'm running the code on Eclipse, I'm putting it here on Sololearn so anyone who can answer can see the code
6th Feb 2023, 9:49 AM
Sibusiso Mbambo
0
"I think you should add a `counter` variable, initialised to zero at the beginning of each month and increasing by 1 for each day, then get the index of alphabet[counter] .. I can't run your code so I'm not sure how it works" On your first suggestion, I already have the counter on my code and it working but the alphabets or characters are not being carried-over to the next month. int count = 0; String[] alphabet = {"|A","|B","|C","|D","|E","|F","|G","|H","|I","|J","|K","|L","|M","|N","|O","|P" ,"|Q","|R","|S","|T","|U","|V","|W","|X","|Y","|Z","|A"}; for(int day =1; day <= numberOfDays; day++) { model.setValueAt("" + day + alphabet[count], i/7, i%7); i = i + 1; if(count == 26) { count = 0; } count++; }
6th Feb 2023, 10:28 AM
Sibusiso Mbambo
0
I Tried implementing and manipulating your suggestion with your attached code but for some reason I'm getting array out of bound exception.
6th Feb 2023, 3:47 PM
Sibusiso Mbambo
0
Lochard I tried doing that but I'm getting ArrayOutOfBoundException
7th Feb 2023, 1:46 PM
Sibusiso Mbambo
0
Mirielle tries and tested but still no solution of the results I want.
7th Feb 2023, 1:48 PM
Sibusiso Mbambo