2

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
41 72 6c
  • 1,600
  • 5
  • 19
  • 30

4 Answers4

3

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.

Smile
  • 3,832
  • 3
  • 25
  • 39
  • That explains it all in one. Thank you very much for your answer. On the one hand the version change was necessary to build my switch case and on the other hand I defined my cases wrong. Thank you very much for your answer. It helped and worked. – 41 72 6c Jun 21 '20 at 14:27
2

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.

mslowiak
  • 1,688
  • 12
  • 25
1

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

Smile
  • 3,832
  • 3
  • 25
  • 39
1

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();
switch (database) {
case "SQLSERVER":
    manager = new CManagingSQLServer();
    break;
case "ORACLE":
    manager = new CManagingOracle();  
    break;
case "MYSQL":
    manager = new CManagingMySQL();
    break;

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