home / study / engineering / computer science / questions and answers / write a
ID: 3861628 • Letter: H
Question
home / study / engineering / computer science / questions and answers / write a program that prompts the user for one of ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: Write a program that prompts the user for one of t... Bookmark Write a program that prompts the user for one of the numbers 1, 2, 3, ..., 12 and outputs the corresponding month name January, February, March, ... , December. It needs to be in String and substring. Also each month should be spaced like every month is 10 spaces and then use s.trim() to clear the white spaces.
Explanation / Answer
import java.util.Scanner;
public class months {
public static void main(String argv[])
{
//declaring array of months...each spaced by ten spaces
String s[]={" January "," February "," March "," April "," May "," June "," July "," August "," September "," October "," November "," December "};
int n;
Scanner sc = new Scanner(System.in);
//reading number from user
System.out.print("Enter number between 1 and 12(inclusive):");//prompting number
n = sc.nextInt();
System.out.println(s[n-1].trim());//printing corresponding month name...and trimming it..
}
}
output:-
Enter number between 1 and 12(inclusive):5
May
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.