IllegalArgumentException = hard to catch it.. (help)

Hi there,
I've got little problem catching the "IllegalArgumentException", if you guys know how to solve this problem that'd help me a lot..
The code works, but if I type something else than what's assigned in Enum it shows IllegalArgumentException...
TIA
       public Options paper, rock, scissors;
     //assign variables
       paper = Options.paper;
       rock = Options.rock;
       scissors = Options.scissors;
     String Playing;
//here comes the code.... = problem................................
//Select one of these only!
while(     !(Options.valueOf(thePlay) == Options.rock) && !(Options.valueOf(thePlay) == Options.paper) && !(Options.valueOf(thePlay) == Options.scissors)  )
         System.out.println("\n It's your turn select ONE:  rock,  paper,  scissors ");
     try{
      catch (IllegalArgumentException e)
          Playing = scan.next().toLowerCase();
.......Error:
Exception in thread "main" java.lang.IllegalArgumentException: No enum const class...

I see.
A few ways occur to me. Given this:
enum Stuff {dog, cat, monkey}You could do this:
Stuff s = null;
do {
  try {
    s = Stuff.valueOf(Input.getString());
  } catch (IllegalArgumentException e) {
    System.err.println("that's not a valid Stuff: " + e);
} while(s == null);
System.out.println("s is: " + s);which works, but is ugly.
Or you could do this:
Stuff s = null;
ASKUSER: while(true) {
  String in = Input.getString();
  for(Stuff t : Stuff.values()) {
    if (t.name().equals(in)) {
      s = t;
      break ASKUSER;
  System.err.println("That's not a valid value: " + in);
System.out.println("s is: " + s);Or you could do this:
Map<String, Stuff> vals = new HashMap<String, Stuff>();
for(Stuff t : Stuff.values())
  vals.put(t.name(), t);
// then later:
Stuff s = null;
do {
  s = vals.get(Input.getString());
  if (s == null)
    System.err.println("That's not a valid value");
} while(s == null);
System.out.println("s is: " + s);which might be worthwhile if you're doing a lot of lookups and there are a lot of values in the enum.
Another way which may be best is the following. First, define your enum like this:
enum Stuff {
  dog, cat, monkey;
  public static Stuff niceValueOf(String s) {
    for(Stuff t : values())
      if (t.name().equals(s))
        return t;
    return null;
}Then you could just do this;Stuff s = null;
do {
  s = Stuff.niceValueOf(Input.getString());
  if (s == null)
    System.err.println("That's not a valid value");
} while(s == null);
System.out.println("s is: " + s);I haven't used enums a whole lot myself, so maybe there's a better approach I'm not familiar with.

Similar Messages

Maybe you are looking for

  • A strange question in me32.

    when change the contract confirm quantity use me32,we cann't change the quantity less then price unit,if i do it ,the confirm quantity auto change to price unit's quantity,when trace the code ,we find this code, H-MENGE = EKPO-PEINH * EKPO-BPUMN * 10

  • How to remove all scripts on object instances?

    I'm looking for any kind of solution here, besides disabling the warning. I hate it when I'm converting someone else's AS2 project to AS3, and there's code buried on an onstage instance somewhere, which gives me the warning: "Actions on button or Mov

  • [SOLVED] OpenOffice with DOCX support

    I have to use sometimes Microsoft Word documents in Microsoft Office 2007 format (.docx) (my boss is fan of this format ), but standart version of OpenOffice doesn't support this format and I should ask my partners to convert it to old Word format. B

  • FM Radio in MP3 Player Not Work

    The FM Radio Autoscan function of my newly bought 256MB MuVo TX FM MP3 player does not seem to be functioning properly. During the st time I performed autoscanning, the player was only able to register a few radio channels. Channels like 93.3FM and 9

  • When I export 1080p XLM from imovie 09 into fce4 it appears as 720x480 ???

    FCE4 really ***** compared to Adobe Premiere!!! Here's my problem. When I export a 1080p file from imovie09 using the XML export function it appears as 720x480 in fce4 ???? Also will the file suffer from the infamous single frame processing that imov