How to print the number of days of each month using Array, String and byte?

1

I'm trying this code, but it does not 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]); 

    }
}
    
asked by anonymous 04.01.2017 / 15:24

2 answers

1

There are some errors in this code. The first array is not declared as an array and the elements are not enclosed in quotation marks. To display 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]);
    }

}
    
04.01.2017 / 15:47
3

You can use package classes java.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.");
}

Running on IDEONE

    
04.01.2017 / 16:07