41 72 6c
41 72 6c

Reputation: 1780

Transforming if-else into switch case throws error [Java]

I tried to convert my if-else statements into a switch case but I had the following problem.

Old code:

if (properties.get("database").toString().equalsIgnoreCase("SQLSERVER")) {
    manager = new CManagingSQLServer();             
} else if (properties.get("database").toString().equalsIgnoreCase("ORACLE")){
    manager = new CManagingOracle();                        
} else if (properties.get("database").toString().equalsIgnoreCase("MYSQL")){
    manager = new CManagingMySQL();                         
} else {
    System.out.println("Not supported DB: " + properties.get("database").toString() + "\n");
    System.out.println("Supported DB:");
    System.out.println("- ORACLE");
    System.out.println("- SQLSERVER");
    System.out.println("- MYSQL");
    System.exit(0);
}

New code:

String database = properties.get("database").toString();
switch (database) {
case database.equalsIgnoreCase("SQLSERVER"):
    manager = new CManagingSQLServer();
    break;
case database.equalsIgnoreCase("ORACLE"):
    manager = new CManagingOracle();  
    break;
case database.equalsIgnoreCase("MYSQL"):
    manager = new CManagingMySQL();
    break;

default:
    System.out.println(database + "is not a supported database.");
    System.exit(0);
    break;
}

First, the String database threw an error that I have to change setting/property (actually don't know) into version 1.7?! After doing so, my cases are throwing now errors. They say: Type mismatch cannot convert from boolean to String.

I read other SO-thread and they said I have to try (String)something or something.ToString(). But both cases didn't work and I don't understand what changed with the above mentioned change to version 1.7. And how can I make my cases work again?

Upvotes: 2

Views: 375

Answers (4)

Nahid
Nahid

Reputation: 98

Java version: 12 -> 17 -> 21 you are missing the whole concept of switch case , you don't have to put equal condtion in your switch case.

just put like this it will work fine

String database = properties.get("database").toString().toUpperCase();

manager = switch (database) {
    case "SQLSERVER" -> new CManagingSQLServer();
    case "ORACLE" -> new CManagingOracle();
    case "MYSQL" -> new CManagingMySQL();
    default -> {
        System.out.println(database + " is not a supported database.");
        System.exit(0);
        yield null;
    }
};

Upvotes: 1

Sabareesh Muralidharan
Sabareesh Muralidharan

Reputation: 647

Use the string value in case statements. Case "SQLSERVER":

Upvotes: 1

mslowiak
mslowiak

Reputation: 1838

The problem you are facing is that in switch you pass a String typed database. In case of section you want to work with boolean expression database.equalsIgnoreCase(...).

The easiest way to deal with that is to change the line:

String database = properties.get("database").toString();

to:

String database = properties.get("database").toString().toUpperCase();

and in case section use simple approach (as you have already upper cased database variable):

case "SQLSERVER"

instead of

case database.equalsIgnoreCase("SQLSERVER")

INFORMATION: Switch expressions that work with strings are available from JDK 7.

Upvotes: 2

Smile
Smile

Reputation: 4088

Change database variable to

String database = properties.get("database").toString().toUpperCase();

And switch case to

case "SQLSERVER":

Currently, you are getting error because database.equalsIgnoreCase("SQLSERVER") returns boolean but you are switching on database which is a String.

Also, you need to use minimum of Java 7 because Java versions before that don't support switch case on String.

Upvotes: 3

Related Questions