How to print the day numbers of each month using Array, String and byte?

I'm trying this code, but it doesn't work.

public class MesDias {

    public static void main(String[] args) {

        // Array String byte
        String Mes = {Jan, Fev, Mar, Abr, Mai, Jun, Jul, Ago, Set, Out, Nov, Dez};
        byte[] Dias = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        for (int x=0; x<Mes.length(); ++x)
        for (int y=0; y<Dias.length; ++y)

        System.out.println(Mes[x] + " " + Dias[y]); 

    }
}
Author: Chewie75, 2017-01-04

2 answers

There are some errors in this code. The first array is not declared as an array and the elements are not enclosed in quotes. To show each respective month with the day, you do not need to make a nested for.

Ex:

public class Datas {

    public static void main(String[] args) {


        // Array String byte
        String[] Months = { "Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul", "Ago", "Set", "Out", "Nov", "Dez" };
        byte[] Days = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

        for (int x = 0; x < Months.length; ++x)
                System.out.println(Months[x] + " " + Days[x]);
    }

}
 1
Author: Júlio César Baltazar, 2017-01-04 15:15:49

Can use package classesjava.time.* to get the number of days of all months of the year, for example:

final int year = 2017;

for(Month month : Month.values()){

    LocalDate date = LocalDate.of(year, month, 01);
    int numberOfDays = date.lengthOfMonth();
    String monthName = month.getDisplayName(TextStyle.FULL, new Locale("pt", "BR"));

    System.out.println(monthName + " tem " + numberOfDays + " dias.");
}

Not working IDEONE

 3
Author: Renan Gomes, 2017-01-04 15:07:32