if(count == 4){
		qFinal.enqueue(q.peek());
		if(count3 == 0){
		System.out.println(qFinal);
		break;
	       }	
		else{			
		    sort(qFinal);
		}		
	}

this code is inside a while loop, and with the break statement, it should break out of the loop right? instead i think the program also executes the sort(qFinal) method b/c after it prints qFinal correctly, the program gives me an error.

Dani AI

Generated

Quick diagnosis and likely causes: a plain break only exits the innermost loop or a switch. If the program prints the queue and then an error appears, common explanations are: the break never executes at runtime (for example count3 wasn’t actually 0), braces/indentation are mismatched so the else belongs to a different if, the break is inside a switch or an inner loop (so it doesn’t exit the loop you expect), or a separate call to sort(...) runs after the loop. was right to point to the stack trace; ’s suggestion to move the break would produce the compiler error described.

Useful checks (instrumentation + examples)

while (someCondition) {
    System.out.println("DBG enter loop: count=" + count + " count3=" + count3);
    qFinal.enqueue(q.peek());
    if (count == 4 && count3 == 0) {
        System.out.println("DBG: about to break; qFinal=" + qFinal);
        break;
    }
    if (count == 4 && count3 != 0) {
        System.out.println("DBG: about to call sort; qFinal=" + qFinal);
        sort(qFinal);
    }
    // other loop work...
}

If an outer loop must be exited from an inner location, use a labeled break or return:

outerLoop:
while (cond) {
    // ...
    if (needToExitAll) break outerLoop;
}

Checklist (what the stack trace will tell and what to verify)

  • Read the stack trace: top frame gives the exact class and line where the exception occurred (inside sort or after the loop).
  • Confirm the break is inside the loop you intend (not inside a switch or inner loop).
  • Verify braces/indentation match the actual block structure.
  • If the exception is in sort, inspect qFinal contents (nulls, mixed types) and any comparator used.
  • Use logging or a debugger to see the actual values of count and count3 at runtime.

These steps will show whether the issue is control flow (break placement) or a runtime error inside sort.

Recommended Answers

All 4 Replies

Okay? So it gives you an error? And? How did you determine that this sort method had anything to do with it, or are you just guessing? Print the stacktrace of the error and read it. It will tell you exactly where the problem is. If you find yourself incapable of zeroeing in on the problem this way, then post the full stack trace here and more code, that little snippet above tells us nothing.

i guess break command should come after if block and before else.

after if and before else? you mean like this?

if ( valid ){
  // code if valid
}
break;
else{
  // code if not valid
}

looks to me like you're setting yourself up to get two problems..
1. you'll run the break no mather whether valid is true or not
2. I kind of think you'll run into an 'else without an if' error

Huh? Try again. Doing that would, of course, lead to an "else without if" compiler message. Please, don't guess, or at least try your guesses before posting them.

Edit: Damn! Too slow, again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.