简体   繁体   中英

Illegal argument exception

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);
    }

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 ...

You probably should wrap your call to

CrimeType newCrime = CrimeType.valueOf(crime);

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

这是用未在枚举中定义的字符串调用valueOf时的预期行为。

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.

eg

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM