Arrays | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Arrays

/*I don't quite understand array. This is my first time seeing them in my life, can you please explain to me what is the error here?*/ Your calendar program should output all the days of week, but it has errors. Change the code so that the program prints the days. 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]); } } }

18th Mar 2021, 8:00 PM
Yeisit
Yeisit - avatar
7 Answers
+ 1
You have to declare arrays by curly braces int[] days = { .. }; //replace with [ ] edit : Array days should be string type as well.
18th Mar 2021, 8:02 PM
Jayakrishna 🇮🇳
+ 1
Oh thanks guys, i’ve tried before replacing the curly braces, but didnt think about the array name. thanks a lot!
19th Mar 2021, 10:50 AM
Yeisit
Yeisit - avatar
+ 1
public class Main { public static void main(String[] args) { String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; //Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U for (int i = 0; i < 7; i++) { System.out.println(days[i]); } } }
3rd Sep 2021, 5:35 PM
Fazal Haroon
Fazal Haroon - avatar
+ 1
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]); } } } Good Luck
26th Jan 2022, 6:25 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
0
As Jayakrishna🇮🇳 writes, you should use curly braces. What's more, there are strings in the array, not numbers. Therefore, you should change the data type from int to String: String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
18th Mar 2021, 8:56 PM
Lukáš Vladař
0
what does the for loop do. I'm doing the same problem why is there and why are we printing it.
1st Apr 2021, 6:27 PM
Chinecherem C Ejikeme
Chinecherem C Ejikeme - avatar
0
The loop is there to iterate through the days of the week. Remember for loops determine how many times a specific piece of code will run. Thus: for (x = 0; x < 7; x++) { System.out.println(days[2]); } will print Wednesday seven times. That should point you in the right direction.
21st Aug 2021, 3:58 PM
Langston McKee
Langston McKee - avatar