tags:

views:

226

answers:

4

When I enter a string that it not in the list, I get this error in my enum CrimeType

class: IllegalArgumentException, no enum const class CrimeType.a(in java.lang.Enum).

What does it mean and how can I fix this?

public void enterCrime()
{
    Crimes crimes = new Crimes();
    System.out.print("\t\tEnter crime: ");
    crimeName = In.nextLine();

    if("murder".equals(crimeName) || "arson".equals(crimeName) || "assault".equals(crimeName))
    {            
        crimes.daysToStay(3);        
    }
    else if("fraud".equals(crimeName) || "theft".equals(crimeName) || "vandalism".equals(crimeName))
    {
        crimes.daysToStay(2);
    }
    else if("drunk".equals(crimeName) || "littering".equals(crimeName) || "badHair".equals(crimeName))
    {
        crimes.daysToStay(1);
    }
    else
    {
        System.out.println("\t\tThat is not a valid crime. The crimes are");
        crimes.list();
    }      
    crimes.add(crimeName);
    enterAction();  
}

Enum Class

public enum CrimeType
{
    murder, arson, assault, fraud, theft, vandalism, drunk, littering, badHair;
}

Crimes Class

import java.util.*;
import java.text.*;
public class Crimes
{
    private LinkedList<CrimeType> crimes = new LinkedList<CrimeType>();    

    public Crimes()
    {       
    }

    public void add(String crime)
    {
        CrimeType newCrime = CrimeType.valueOf(crime);
        crimes.add(newCrime);
    }
+1  A: 

You probably should wrap your call to

CrimeType newCrime = CrimeType.valueOf(crime);

in a try/catch block. This is where the code is failing.

David Andres
+1  A: 

That is the expected behavior of calling valueOf, when called with a string that is not defined in the enum.

Zed
+4  A: 

It means what you said in the first line "a string that it not in the list,"

CrimeType.valueOf(crime) will return a CrimeType matching the string crime.

Javadoc for valueOf says that if there is no constant with the specified name it will throw an IllegalArgumentException.

You then in the code have to decide what to do when the string does not match a enum.

First step to to enclose the valueOf in a try catch block and then in the catch do something could be print out that you have an unknown string or could do nothing or ...

Mark
+1  A: 

Your current problem lies in the fact that after determining the line entered isn't valid you still try to look it up... leading directly to the IllegalArgumentException.

However, as an aside, why have the extra logic anyway? The enum can quite happily contain the daysToStay data.

e.g.

public void enterCrime() {
    Crimes crimes = new Crimes();
    System.out.print("\t\tEnter crime: ");
    crimeName = In.nextLine();

    try { 
        crimes.add(crimeName);

    } catch (IllegalArgumentException e) {
        System.out.println("\t\tThat is not a valid crime. The crimes are");
        crimes.list();

    }      
    enterAction();  
}

public enum CrimeType {
    murder(3), arson(3), assault(3), 
    fraud(2), theft(2), vandalism(2), 
    drunk(1), littering(1), badHair(1);

    private final int daysToStay;

    CrimeType(in daysToStay) {
        this.daysToStay = daysToStay;
    }

    public int getDaysToStay() {
        return daysToStay;
    }
}
Michael Rutherfurd