How to use switch with enum for string

java switch case enum string example
enum in switch case java 8
string to enum java
an enum switch case label must be the unqualified name of an enumeration constant
how to use enum in switch case c#
enum valueof
switch statement java enums
java switch enum constant expression required

Sorry for the extremely basic question, but I want to figure out how to use a switch statement with cases that check if it's a certain string.

For example if I have an AnimalType enum and then I have an animal struct:

enum AnimalType: String {
   case Mammal = "Mammal"
   case Reptile = "Reptile"
   case Fish = "Fish"
}

struct Animal {
   let name: String
   let type: String
}

If I want to go through a list of Animals and then have a switch statement, how would I match the Animal.Type string to the enum? I don't want to change the Animal struct to let type: AnimalType either.

switch Animal.type {
case :
...// how do I match the string to the enum?

You can create an animal type from string rawValue and switch on that: But first I'd change the cases to lowercase, thats the preferred style in Swift.

func checkType(of animal: Animal) {
    guard let animalType = AnimalType(rawValue: animal.type) else {
        print("Not an animal type")
        return
    }
    switch animalType {
    case .mammal: break
    case .reptile: break
    case .fish: break
    }
}

Alternatively, you can also switch on the string and compare if it matches any of your AnimalType rawValues:

func checkType(of animal: Animal) {
    switch animal.type {
    case AnimalType.mammal.rawValue: break
    case AnimalType.reptile.rawValue: break
    case AnimalType.fish.rawValue: break
    default:
        print("Not an animal type")
        break
    }
}

How to use Java Enum in Switch Case Statement, values() method and by using advanced for loop of Java 5, we loop through each of that Enum instance. By the way, you can also use String in Switch case from  I would like to use an enum value for a switch statement. Is it possible to use the enum values enclosed in "{}" as choices for the switch()"? I know that switch() needs an integer value in order to direct the flow of programming to the appropriate case number. If this is the case, do I just make a variable for each constant in the enum statement?

enum Command: String {
   case Mammal = "Mammal"
   case Reptile = "Reptile"
   case Fish = "Fish"
}

let command = Command(rawValue: "d")

switch command {
case .Mammal?:
    print("Mammal")
case .Reptile?:
    print("second")
case .Fish?:
    print("third")
case nil:
    print("not found")
}
// prints "not found"

Can we use Switch statement with Strings in java?, must not be null else, a NullPointerException is thrown (Run-time). You can get an enum from a string with Enum.valueOf(). Take care here, the other answers fail to mention that Enum.valueOf() will throw an IllegalArgumentException if passed an string which isn't a valid member of the enum. Be sure to properly format and indent your code, it helps us (and you!) read it and understand what's going on:

I'd suggest you to use enum for comparison and making it optional for the type. You could also use rawValue of the enum to compare them.

enum AnimalType: String { 
    case Mammal //No need of case Mammal = "Mammal"
    case Reptile
    case Fish
}

struct Animal {
    let name: String
    let type: AnimalType?
}


let lion = Animal(name: "Lion", type: .Mammal)

switch lion.type {

case .Mammal?:
    break
case .Reptile?:
    break
case .Fish?:
    break
case nil:
    break

}

EDIT: As Matthew in the comment said, if you’re getting the objects from a server, you need to have custom decoding process to convert the string response to corresponding AnimalType enum for comparison. Otherwise you’re good with using just the enum.

Convert String to equivalent Enum value, In this enum tutorial, I want to just focus on using an enum in a switch public static void main(String[] args) { // loop through the enum values,  Enumeration (enum) in Java is a datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year etc. enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } You can also define an enumeration with custom values to the constants declared.

If the strings are exactly the same as the enum, you don't have to define it. Following is my working solution.

enum SegueIdentifier: String {
    case pushViewController1
    case pushViewController2
    case pushViewController3
}

switch SegueIdentifier(rawValue: segue.identifier ?? "") {
case .pushViewController1:
     if let viewController1 = segue.destination as? ViewController1 {
         viewController1.customProperty = customPropertyValue
     }
case .pushViewController2:
     if let viewController2 = segue.destination as? ViewController2 {
         viewController2.customProperty = customPropertyValue
     }
case .pushViewController3:
     if let viewController3 = segue.destination as? ViewController3 {
         viewController3.customProperty = customPropertyValue
     }
default:
    let message = "This case has not been handled."
    let alertController = UIAlertController(title: "Alert", message: message, preferredStyle: .alert)
    let okayAction = UIAlertAction(title: "Okay", style: .default) { (_:UIAlertAction) in }

    alertController.addAction(okayAction)

    present(alertController, animated: true, completion: nil)
}

how do I use an enum value on a switch statement in C++, Can we use String in Switch Case, We can't use String in Switch case prior to Java7 but solution is Java Enum. How to use Java Enum in  You can't use a switch statement to do the string conversion as each case must be a constant and Colors.red.ToString() is not. An alternative is to convert the string to an enum first and then a switch statement to compare.

A Java enum switch/case statement example, enum Car { lamborghini,tata,audi,fiat,honda } public class Main { public static void main(String args[]){ Car c; c = Car.tata; switch(c) { case lamborghini:  Problem Description. How to use enum & switch statement ? Solution. This example displays how to check which enum member is selected using Switch statements.

How to use Java Enum in Switch Case Statement, This example show you how to use enumeration or enum type in a color) { String colorCode = ""; // We use the switch-case statement to get  public enum MethodType { DOUBLE,LIST,STRING,ARRAYLIST,FLOAT,LONG; } In a property file, I've key value pairs as follows. Test1=String Test2=Double. In my code I'm getting the value for the key. I need to use the VALUE in Switch Case to Determine the Type and based on that I've to implement some logic. For example something like this

Java Examples - Use of enum & switch statement, We call it by using String.equals(otherString) and it returns a boolean value (true / false) as to whether or not the two strings are equal. If you're  Now, let's say I want to display the string value of enum in some control. For that, I will have to convert Enum value to a string. The following code loops through enum and adds string values to a DropDownList.

Comments