0

outprint all weekdays(July)

Hello, I want to outprint all the (31) weekdays of July, but I haven't been able to find the solution. Here is my code: package testPackage; public class testClass { public static void main(String[] args) { System.out.println("\tDay"); String[] arr = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; int july = 31; for(int counter=1;counter<july;counter++) { System.out.printf("%n"+ counter + "\t" + arr[counter]); } } } Output: Day 1 Mon 2 Tue 3 Wed 4 Thu 5 Fri 6 SatException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at testPackage.testClass.main(testClass.java:14)

6th Jul 2018, 4:22 PM
Michael Honer
Michael Honer - avatar
2 ответов
+ 5
Your counter goes beyond 6, which is the maximum you can use as an index in arr. Use counter%7 so your index wraps around to zero.
6th Jul 2018, 5:47 PM
John Wells
John Wells - avatar
+ 1
Thank you for your reply. I will try it out as soon as I come home
9th Jul 2018, 9:47 AM
Michael Honer
Michael Honer - avatar