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

167
Visualizações
Java Skipping switch statement using getter as expression

Hey Sorry for the what for you guys must seem like a stupid question but I've been stuck on this for a while now, im using a getter from a different class as the switch expression in another class, my program keeps skipping the entire switch alltogether.

Thank you for your time!

First class


public class InputRequests {
    public int loginRole;


    public void loginChoice(){
        Scanner login = new Scanner(System.in);
        loginRole = login.nextInt();
    }

    public int getLoginRole() {
        return loginRole;
    }
} 

Second class

public class Menus {

    public void loginChoiceCase() {
        InputRequests inp = new InputRequests();
        switch (inp.getLoginRole()) {
            case 1 -> System.out.println("test");
            case 2 -> System.out.println("tested");
            case 3 -> System.exit(1);
        }
    }
}

main

public class Main {
    public static void main(String[] args) {
        Printblocks print = new Printblocks();
        InputRequests input = new InputRequests();
        Menus menu = new Menus();
        print.firstMenu();
        input.loginChoice();
        menu.loginChoiceCase();
        System.out.println(input.getLoginRole());
    }
}

The first menu print is just a pintblock without anything else, when running the program I do get the int I enter back.

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

0

Your program isn't skipping over the switch statement. Look at the Menus.loginChoiceCase() method. It's creating a new instance of InputRequests. Which means inp.getLoginRole() is null, and your switch statement doesn't have a default case to catch nulls.

Add a default case to the switch statement

default -> System.out.println("Null");

Ideally what you would want to do is have your loginChoiceCase() method take in an int, and then use that in you case statement. With loginChoiceCase() able to take in an int you can then use your getter. Like below.

InputRequests

public class InputRequests {
public int loginRole;


public void loginChoice(){
    Scanner login = new Scanner(System.in);
    loginRole = login.nextInt();
}

public int getLoginRole() {
    return loginRole;
}
}

Menus

public class Menus {

public void loginChoiceCase(int choice) {
    switch (choice) {
        case 1 : System.out.println("test");
        case 2 : System.out.println("tested");
        case 3 : System.exit(1);
        default : System.out.println("Null");
    }
}
}

Main

public class Main {
public static void main(String[] args) {
    Printblocks print = new Printblocks();
    InputRequests input = new InputRequests();
    Menus menu = new Menus();
    print.firstMenu();
    menu.loginChoiceCase(input.getLoginRole);
    System.out.println(input.getLoginRole());
}
}
over 4 years ago · Santiago Trujillo Relatório

0

You need to remove this line because this is a different object from what you declared in the main method

public class Menus {

    public void loginChoiceCase() {
        InputRequests inp = new InputRequests(); <--- delete this line
        switch (inp.getLoginRole()) {
            case 1 -> System.out.println("test");
            case 2 -> System.out.println("tested");
            case 3 -> System.exit(1);
        }
    }
}

Explanation: why this happened?

Because the object you declared in the loginChoiceCase method has didn't store the value from the user console and the value of loginRole the default which is zero and you don't have a menu with zero case.

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