Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: problem with enum (int to months)

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default problem with enum (int to months)

    My assignment is to ask the user to enter a number 1-12 and you will print out the the name of the month. You MUST use enumerated types to associate the number entered to the name of the month.

    This is what I have so far
    import java.util.*;
    public class Assignment4{
    enum Month{January, February, March, April, May, June, July, August, September, October, November, December}
     
    public static void main(String[]args){
    int in=getInput();
    for (Month m: Month.values()){
    System.out.printf("Month %s = %s\n",in , m);
    }//end of for
    }//end of main
    public static int getInput (){
    Scanner input = new Scanner (System.in);
    System.out.println("Please, enter a number (1-12) of the month you want me to print. ");
    int in = input.nextInt();
    return in;
    }//end of getInput
    }//end of class

    The problem is no matter what number I put in, it prints out the entire enum. I realize this is because I'm using Month.values() However, anything else I put in errors out. I did read
    http://www.javaprogrammingforums.com...use-enums.html
    I tried some of the other methods but I guess I'm not entering something correctly


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: problem with enum (int to months)

    From the Enum Types page on Oracle's Tutorial, enums "have a static values method that returns an array containing all of the values of the enum in the order they are declared".

    You could print out the appropriate element from that array.

Similar Threads

  1. numbered months
    By JavaN00b101 in forum Java Theory & Questions
    Replies: 5
    Last Post: February 8th, 2012, 01:13 PM
  2. Replies: 0
    Last Post: January 12th, 2012, 07:00 AM
  3. Help with simple enum problem
    By Prox in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 22nd, 2011, 03:19 PM
  4. Declaring enum problem
    By kumalh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 5th, 2011, 06:30 AM
  5. [SOLVED] Enum problem
    By pajfilms in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 21st, 2011, 07:26 AM