Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

217
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

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

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda