0
Arrays - Fixing the Calendar
Your calendar program should output all the days of week, but it has errors. Change the code so that the program prints the days. Existing code: public class Main { public static void main(String[] args) { int[] days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; for (int i = 0; i < 7; i++) { System.out.println(days[i]); } } } I tried changing the square brackets to curly brackets for Monday etc but it doesnât work. Please help.
4 Answers
+ 1
Chin Eu
Syntax for declaring array:
int[] arr = { };
As Monday, Tuesday....are string so you have to declare string array not int array.
String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
for (int i = 0; i < 7; i++) {
System.out.println(days[i]);
}
+ 5
String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
0
Thanks a lot Avinesh and Ajanant !
0
public class Main {
public static void main(String[] args) {
String [] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
for (int i = 0; i < 7; i++) {
System.out.println(days[i]);
}
}
}