Write a Java program that transforms numbers 1 , 2 , 3 , …, 12 into the corresponding month names January , February , March , …, December . Hint: Make a very long string "January February March ..." , in which you add spaces such that each month name has the same length. Then use substring to extract the month you want.
Code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String names;
String month_text;
System.out.print("Month number: ");
int month = input.nextInt();
input.close();
names = "January February March April May June July August SeptemberOctober November December ";
month_text = names.substring((month - 1) * 9, month * 9);
System.out.println(month_text);
}
}