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

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

You entered "a" at the prompt, didn't you?

According to your enum class these

murder, arson, assault, fraud, theft, vandalism, drunk, littering, badHair

are the valid entries.

You might wish to wrap the line

CrimeType newCrime = CrimeType.valueOf(crime);

in a try catch block so that you can simply inform the user that (s)he entered an invalid value rather than having that crash your program.

where should I put the catch statement? because the else statement is in the prisoner class, and I want it to print that out after it catches the exception.

ahhh I got it! I should just put nothing inside the catch statement. Thanks a lot!!!

public void add(String crime)
    {
        try
        {
            CrimeType newCrime = CrimeType.valueOf(crime);
            crimes.add(newCrime);
        } 
        catch(IllegalArgumentException e)
        {
        }
    }

Uhm, you should put something in there even if only a log statement. Another possibility is to make the method boolean as follows:

public boolean add(String crime) {
        try {
            CrimeType newCrime = CrimeType.valueOf(crime);
            crimes.add(newCrime);
        } catch(IllegalArgumentException e) {
            return false;
        }
        return true
    }

That way, you at least know something went wrong and can inform the user, and you never want to simply ignore Exceptions (well there are a few places where you can, although you should still, at least, log them).

Edit: Then change this

} else {
            System.out.println("\t\tThat is not a valid crime. The crimes are");
            crimes.list();
        }
        crimes.add(crimeName);

to this

}
        if (!crimes.add(crimeName)) {
            System.out.println("\t\tThat is not a valid crime. The crimes are");
            crimes.list();
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.