• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enumerations

 
Tom Orr
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would help if you describe the trouble you are having.
 
vicky Aggarwal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[pedantic mode]That's not an Enumeration. That's an enumerated type[/pedantic mode] You may find those two links useful
 
Tom Orr
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic