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

220
Visualizações
Infinite Output in case Statement java

I am accepting input from user in the form of a string (which contains both char & int) .I am further dividing string into char & int and using char in case statement,but my case statement giving me infinite loop.I am not able to figure out the error..

public static void main(String[] args) {
    
    Scanner sc=new Scanner(System.in);
    
    String str=sc.nextLine();
    
        String[] part = str.split("\\s+");
        
      do 
      { 
          
         switch(part[0])
         {
          case "AB":
              System.out.println("hi");
              int data=Integer.parseInt(part[1]);
              System.out.println(data);
              break;
          case "PR":
              System.out.println("printining");
              break;
          case "AE":
              System.out.println("AE");
               break;
          case "EXIT":
              System.exit(0);
              break;
         }
      }
      while(true);
        
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You don't change part inside the loop; so unless you enter EXIT initially, it's going to keep looping.

Move the two lines above the loop into the loop:

do
{
    String str = sc.nextLine();

    String[] part = str.split("\\s+");

    // Rest of loop body.
over 4 years ago · Santiago Trujillo Relatório

0

break statement quit the switch not the loop, and the loop condition is ALWAYS true!

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