Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
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 answers
Answer question

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 Report

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!