Arrays | Sololearn: Learn to code for FREE!

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]); } } }

3/18/2021 8:00:29 PM

Yeisit

7 Answers

New Answer

+2

You have to declare arrays by curly braces int[] days = { .. }; //replace with [ ] edit : Array days should be string type as well.

+1

Oh thanks guys, i’ve tried before replacing the curly braces, but didnt think about the array name. thanks a lot!

+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]); } } }

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"};

0

Think of arrays as groups of storage containers. A single dimensioned array is just a long line of boxes that are numbered starting from 0. An individual box can be identified by a single number. A two dimensional array is like a storage rack of boxes. To identify an individual box requires two numbers, the shelf number and the box number on that shelf. A three dimensional array is like having rows of shelves. An individual location requires three numbers to identify it. The row number, the shelf number, and the box number. A four dimensional array is like identifying the warehouse, row, shelf, box. etc. etc. etc.

0

what does the for loop do. I'm doing the same problem why is there and why are we printing it.

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.