
November 9th, 2012, 12:47 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 1
Time spent in forums: 8 m 58 sec
Reputation Power: 0
|
|
|
[Homework] month name to 3 letter abbrevation
Hi all,
So i've been struggling with this homework that is convert a month to a 3 letter abbreviation. I i've done many searches that month month numbers to names, abbreviations to months etc, but not what I need.
Here is the number to date from my textbook:
Code:
/** MonthNumbers.java reads a 1-12 number and displays
* the three-letter abbreviation of the corresponding month.
*
* @author Joel Adams, for Alice+Java
*/
import java.util.Scanner;
public class MonthAbbreviations {
public static void main(String[] args) {
// get the month number
System.out.println("To see the first three letters of a month,");
System.out.print(" enter a month-number (1-12): ");
Scanner kbd = new Scanner(System.in);
int monthNumber = kbd.nextInt();
// compute the month abbreviation
final String monthTable = "JanFebMarAprMayJunJulAugSepOctNovDec";
int start = (monthNumber - 1) * 3;
int stop = start + 3;
String monthAbbrev = monthTable.substring(start, stop);
// display the month abbreviation
System.out.println("\nMonth #" + monthNumber +
" begins with '" + monthAbbrev + "'.");
}
}
I'm wondering if name to abbreviation is similar with a few modifications to this? Thanks in advance for any help.
|