#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep 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.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Please edit your post and wrap the code in code tags.

    Look at using an array to hold the month names instead of a String that you have to index into.
    name to abbreviation
    Can you give some examples of that. What are the cases where the abbreviation is not just the first 3 letters?
  4. #3
  5. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    You might consider using a Map.

IMN logo majestic logo threadwatch logo seochat tools logo