aspose file tools*
The moose likes Beginning Java and the fly likes enumerations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "enumerations" Watch "enumerations" New topic
Author

enumerations

Tom Orr
Greenhorn

Joined: Mar 17, 2011
Posts: 13
I am taking a beginning Java class online and am having a little trouble creating and calling methods and including my enum in my method. Here is what I have. any pointers and tips would be greatly appreciated. I am just beginning to learn this so forgive me if I made alot of newbie errors.



Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
It would help if you describe the trouble you are having.
vicky Aggarwal
Ranch Hand

Joined: May 15, 2009
Posts: 40
First thing you can not declare a enum inside a method.
Second if you want to bind enum constants with values try enum with constructors.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

Welcome to the Ranch Tom!

Please UseCodeTags when you post source code - that way, the forum software will format your source code in a nice way and with line numbers, so that it's easier to read. Being a moderator, I edited your post above and added the code tags.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
[pedantic mode]That's not an Enumeration. That's an enumerated type[/pedantic mode] You may find those two links useful
Tom Orr
Greenhorn

Joined: Mar 17, 2011
Posts: 13
Thank you everyone for your responses. Vicky the assignment asks us to place our enum in our method. Here is what is requested.

1. Create a method for the conversion.
2. Use an enumeration to define the possible months (place in the conversion method).
3. Print an error message to the screen if the input is out of bounds.
4. Wrap the user input in a while loop. Continue looping until the user quits. Put this part in the main method and then pass the user input to the conversion method to convert from an integer to a month.


Are you saying what is requested in step 2 is not possible?

I took a basic programming class back in the 80's and I think my brain is still thinking in basic code and giving me trouble trying to grasp some of the java programming techniques.


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

I don't think they're asking you to define the enum in the method - which isn't possible. It makes more sense that they're asking you to define the enum in an appropriate place, and then use it in the method.
Tom Orr
Greenhorn

Joined: Mar 17, 2011
Posts: 13
OK I think I figured out the Enum issue. I saved my enum as a seperate .class file

Now I get an error telling me variable sMonth might not have been initialized






Edwin Torres
Ranch Hand

Joined: Mar 19, 2011
Posts: 55

Tom Orr wrote:OK I think I figured out the Enum issue. I saved my enum as a seperate .class file
Now I get an error telling me variable sMonth might not have been initialized


You need to initialize the sMonth variable in your setMonth method. Since that method could potentially be given a bad input (e.g. 13), sMonth would be left uninitialized. Set it to null and the error will go away. You should really validate the iMonth parameter in your method though.


Twitter: @realEdwinTorres
Blog: java Friendly
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Edwin Torres wrote: . . . the error will go away. . . .
. . . but you will get another problem in the next block. If you only think there are twelve months in the year, a far better way to handle month 13 or -1 is like this
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You also realise your SetMonth() method does not set the month at all? Also it should be called getNameFromMonthNumber or similar: start with a small letter, and it's not a straight "set" method.
 
jQuery in Action, 2nd edition
 
subject: enumerations