Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Basics Perfecting the Prototype Conditional ints

Here, I have been asked to exit the program. I have mentioned break but its gives me error. Please help

Please let me know what is wrong I am doing

Conditional.java
int numberOfPeople = 3;
if(numberOfPeople < 4){
  console.printf("Your table is ready");
  break;
}

1 Answer

andren
andren
28,558 Points

The break keyword is used to force a loop to exit, it is not used to make the program itself exit. To force the program to exit you have to call System.exit and pass it an exit code. Like this:

int numberOfPeople = 3;
if(numberOfPeople < 4){
  console.printf("Your table is ready");
  System.exit(0); // Exit with code 0 (Which indicates that nothing went wrong)
}

Thanks. It helps.