0

The follow java code will print the numbers 1- 10 starting at 1. How can I use this same code structure to print out all of the months of the year only once so January, February, March, etc?

public class s {
    public static void main(String[] args) {
        for(int i = 1; i <= 10; i++) {
            System.out.println(i);          
        }
    }
}
Andrew Li
  • 55,805
  • 14
  • 125
  • 143

5 Answers5

6

If you want to relay on java api use:

public static void main(final String[] args) {
    // java 8
    Arrays.stream(Month.values())
          .forEach(System.out::println);

    // java < 8
    for (final Month month : Month.values()) {
        System.out.println(month);
    }
}
staszek
  • 251
  • 2
  • 9
2

If you want to do this the old fashion way and a little more hands on for learning arrays:

String[] months = new String[] { "January", "February", "March" };

for (int i = 0; i < months.length; i++) {
    System.out.println(months[i]);
}

Or to be a little more concise:

for (String month : months) {
    System.out.println(month);
}

You can learn more about arrays, and check out some tutorials in the documentation.

Drew Kennedy
  • 4,118
  • 4
  • 24
  • 34
1

Month

The best correct Answer is by staszek making clever use of Streams. Here's the same idea but in old-fashioned syntax instead of Streams, plus some comments.

The Month enum defines a dozen instances, one per month of the year, numbered sanely 1-12 for January-December. The Month.getValues method returns an array of all twelve instances. We can loop that array using the Java syntax for the ”enhanced for loop” also known as a “for-each”.

You can ask for a localized name of the month via the getDisplayName method. Specify a TextStyle for length of abbreviation, and a Locale for the human language and cultural norms used in translating.

TextStyle ts = TextStyle.FULL;
Locale l = Locale.CANADA_FRENCH;

for ( Month month : Month.values () ) {
    int monthNumber = month.getValue (); // 1-12.
    String monthName = month.getDisplayName ( ts , l );
    System.out.println ( "month: " + month + " | monthNumber: " + monthNumber + " | monthName: " + monthName );
}
month: JANUARY | monthNumber: 1 | monthName: janvier
month: FEBRUARY | monthNumber: 2 | monthName: février
month: MARCH | monthNumber: 3 | monthName: mars
month: APRIL | monthNumber: 4 | monthName: avril
month: MAY | monthNumber: 5 | monthName: mai
month: JUNE | monthNumber: 6 | monthName: juin
month: JULY | monthNumber: 7 | monthName: juillet
month: AUGUST | monthNumber: 8 | monthName: août
month: SEPTEMBER | monthNumber: 9 | monthName: septembre
month: OCTOBER | monthNumber: 10 | monthName: octobre
month: NOVEMBER | monthNumber: 11 | monthName: novembre
month: DECEMBER | monthNumber: 12 | monthName: décembre

See Oracle Tutorials for:

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Where to obtain the java.time classes?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Would you try this

String[] months = new DateFormatSymbols().getMonths();
for (int i = 0; i <12; i++) {
     System.out.println("month = " + months[i]);
}
Baker1562
  • 337
  • 3
  • 20
0

Try this solution:

String[] months = new DateFormatSymbols().getMonths();
    for(String singleMonth: months){
        System.out.println(singleMonth);
    }
apajak
  • 21
  • 4