Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
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.

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!

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

AFAIU you problem, a usual approach to this is to have 2 arrays

char[] from = {'a','b','c'};
char[] to = {'1','e','@'};

Read your input char by char, look up the position of the char in from and replace it with the char from the same position in to.

Reverse, the same (to -> from).

over 4 years ago · Santiago Trujillo Relatório

0

I'm just suggesting a way of solving the problem. You can use a Map<Character, Character> for encryption and another for decryption. You can adapt the following program to your needs.

import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main (String[] args) {
        Map<Character, Character> encryptionMap = new HashMap<>();
        encryptionMap.put('a', '-');
        encryptionMap.put('b', '+');
        encryptionMap.put('-', '1');
        encryptionMap.put('+', '3');
        
        String plainText = "a+b-a";
        String encryptedText = "";
        for (int i = 0; i < plainText.length(); i++) {
            char ch = plainText.charAt(i);
            encryptedText += encryptionMap.get(ch);
        }
        System.out.println("Plain Text: " + plainText);
        System.out.println("Encrypted Text: " + encryptedText);

        Map<Character, Character> decryptionMap = new HashMap<>();
        decryptionMap.put('-', 'a');
        decryptionMap.put('+', 'b');
        decryptionMap.put('1', '-');
        decryptionMap.put('3', '+');

        String decryptedText = "";
        for (int i = 0; i < encryptedText.length(); i++) {
            char ch = encryptedText.charAt(i);
            decryptedText += decryptionMap.get(ch);
        }
        System.out.println("Decrypted Text: " + decryptedText);
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda