Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to use an array value as a switch case value?

I am trying to create a working Java code. I created an encryption function that takes any symbol in a string and replaces it with a value from an array.

The problem is very simple. Decryption function must take any value written in an array and use it to decrypt. The code describes it better than I do.

   case "1" -> output += n_encryptions[0];
// Then in decrypt():
   case n_encryptions[0] -> output += "1";
// Sadly, this creates an error

My goal is to make an easily changeable encryption and decryption system, since I am planning to change these values in the future.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I need a way to use an array value (n_encryptions[0] in the example) as a case parameter.

Edit: I guess an if statement is the best option. Thanks!

>Solution :

Is it possible to use an array value as a switch case value?

No, it is not.

Switch labels (to the right of the word case) must be compile-time constant expressions.

I need a way to use an array value (n_encryptions[0] in the example)
as a case parameter.

Why do you ‘need’ this? It surely can be expressed another way,for example by using if.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading