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)
2 odpowiedzi
+ 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.
+ 1
Thank you for your reply. I will try it out as soon as I come home