-1

I'm looping through an enum to find a certain value. If it finds it it continues the program. Else, it closes the program. My current algorithm is checking if all of the values are equal instead of continuing when one is equal. How do I make it continue if it finds one equal value.

    public enum stuff{


    //
    apple("apple.png"),
    banana("banana.png"),
    spinach("spinach.png");

    //


    //Variables
    private String Path;


    //Constructor
    Enemies (String path) {
        Path = path;
    }

    public String getPath() {
        return Path;
    }

}

Actual loading done in another class

String stuffType = banana;
for (stuff s : stuff.values()) {
            if(stuffType != s.name()){ //checks if any are a banana
                System.exit(0);
            }
        }
Joe
  • 355
  • 3
  • 8

1 Answers1

0

You could use a loop, or you could use the valueOf method, which looks up an enum constant by name already, and throws an IllegalArgumentException if there is no enum constant with that name.

String stuffType = "banana";

try {
    stuff.valueOf(stuffType);
} catch(IllegalArgumentException e) {

    // stuff doesn't contain an enum constant with a name that matches the value of stuffType
    System.exit(0);
}
user253751
  • 57,427
  • 7
  • 48
  • 90
  • Thanks, the first method someone showed (someone deleted it) worked for me. I added a "completedSearch" boolean. – Joe Feb 13 '15 at 23:22
  • My first method? Someone else posted one then deleted it, but that wasn't me. – user253751 Feb 13 '15 at 23:22