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

248
Visualizações
how should i put my method(with array) in a switch statement in java?

whats the right way to put a method(with array code) in a switch statement?

    public static void  prtEmployees(String empNo[], String Name[], int rate[]) {
    System.out.println("Employee No.    Employee Name   Rate/Hour");
    for(int i = 0; i < empNo.length; i++) {
        System.out.printf("%s: %s", empNo[i], Name[i], rate[i]);
    }

}
private void Action(int choice) {
switch(choice) {
case 1:
    prtEmployees(); // im having an error here
    break;
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The problem in your code is not about using a switch, but calling a method with a wrong amount of parameters. The method prtEmployees requires 3 parameters:

  1. String array of employee no's
  2. String array of employee names
  3. Int array of employee rates

The 3 parameters are always required to be placed.

Based on your code snippet, I can't see where your input is, where your employee data is coming from. But I created a sample where I call your method in a main class with some dummy data to give an example:

public class Main {
    public static void main(String[] args) {
        String[] employeeNos = {"1", "2", "3"};
        String[] names = {"Alfred", "James", "Siegfried"};
        int[] rates = {2,3,5};

        Action(1, employeeNos, names, rates);
        Action(2, employeeNos, names, rates);
    }

    public static void  prtEmployees(String[] employeeNumbers, String[] names, int[] rates) {
        System.out.print("Employee No.\tEmployee Name\tRate/Hour\n");
        for(int i = 0; i < employeeNumbers.length; i++) {
            System.out.printf("%s.\t%s\t%d\n", employeeNumbers[i], names[i], rates[i]);
        }

    }
    private static void Action(int choice, String[] employeeNos, String[] names, int[] rates) {
        switch (choice) {
            case 1:
                prtEmployees(employeeNos, names, rates);
                break;
            case 2:
                System.out.println("No employee print because of choice 1");
                break;
            default:
                throw new IllegalArgumentException("Only choice 1 and 2 are allowed");
        }
    }
}

This gives as output:

Employee No. Employee Name Rate/Hour

  1. Alfred 2
  2. James 3
  3. Siegfried 5 Only choice 1 and 2 are allowed

Explanation First I call the action method with choice 1 and some dummy data, which will pass the dummy data to the print method and it will print out the 3 columns of employee data. Then I call the same method with choice value 2, which prints an additional single line, just to illustrate the functionality of it. Just for fun I added a default behaviour that other values than 1 and 2 will throw an exception.

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