0

Can someone explain how I can end the program after hitting the 'capacity exceeded' message without using a break or system.exit, but continue to prompt for 'leaving' and 'entering' if the message is not reached?

Also, for the 'capacity exceeded' message it also displays the totalPeople. totalPeople in this section becomes however many people I enter to leave or enter. How can I make so it's the totalPeople stored before I enter the leave or enter values to make it exceed capacity?

 int numLeaving = Integer.parseInt(JOptionPane.showInputDialog("number leaving"));
 int numEntering = Integer.parseInt(JOptionPane.showInputDialog("number entering:"));  
 while (totalPeople <= 65 && totalPeople >= 0) {        
     try  {       
         if (numLeaving >= 0 && numEntering >= 0) {
             totalPeople = (totalPeople + numEntering) - numLeaving;
             JOptionPane.showMessageDialog(null,"Total people = " + totalPeople);                                         
         }          
         else {                         
             JOptionPane.showMessageDialog(null,"Invalid data");
         }
     }
     catch (NumberFormatException e) {
         JOptionPane.showMessageDialog(null,"Enter numbers only");
     }

     if (totalPeople > 65) {
         JOptionPane.showMessageDialog(null,"Capacity exceeded\n" + "Total people = " + totalPeople);
     }     
     numLeaving = Integer.parseInt(JOptionPane.showInputDialog("number leaving"));
     numEntering = Integer.parseInt(JOptionPane.showInputDialog("number entering:"));                    
} 
Jashaszun
  • 9,207
  • 3
  • 29
  • 57
dan
  • 127
  • 9
  • 1
    Don't want to troll but what's wrong with `break` or `System.exit`? –  Jun 15 '15 at 22:16
  • @RC. Look at this: http://stackoverflow.com/questions/3922599/is-it-a-bad-practice-to-use-break-in-a-for-loop – Francisco Romero Jun 15 '15 at 22:17
  • I totally agree with `System.exit` but in this case, I don't see any wrong with `break` (and IMHO a `break` is cleaner than loop-var = some value to exit) –  Jun 15 '15 at 22:19
  • @RC. but `break` statement it's also a bad practice for programming. It's better to put a condition in the loop to exit it instead of a `break` statement. But it's just an opinion. – Francisco Romero Jun 15 '15 at 22:32

3 Answers3

1

To do not continue looping, we have to make the looping condition false:

if (totalPeople > 65) {
    JOptionPane.showMessageDialog(null,"Capacity exceeded\n" + "Total people = " + totalPeople);
    totalPeople  = -1;
} 

The rest of loop code will be executed, but it will not entering while loop again.

If you want to keep the value of totalPeople, the easiest way is to add a boolean variable and use it in the condition of looping:

boolean exit= false;
while (totalPeople <= 65 && totalPeople >= 0 && !exit) { 
// ..
  if (totalPeople > 65) {
         JOptionPane.showMessageDialog(null,"Capacity exceeded\n" + "Total people = " + totalPeople);
         exit= true;
     }
// ..
54l3d
  • 3,913
  • 4
  • 32
  • 58
1

I think you should save your limit of people that could enter in a constant. Like this:

static final int maximumPeople = 65;

So now you can use it for conditions in your loops and whatever you want:

while (totalPeople <= maximumPeople && totalPeople >= 0) 
{      
   //code
}

if (totalPeople > maximumPeople) {
   //code
}

And another variable that you are going to modify, in your case, totalPeople. In this case, you can show your message of the total of people that can enter:

if (totalPeople > maximumPeople) 
{
   JOptionPane.showMessageDialog(null,"Capacity exceeded\n" + "Total people = " + maximumPeople);
}     

But it also will leaves your loop because you are using totalPeople (the real number of people that has entered).

I expect it will be helpful for you!

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
-1

Put the current code in a while loop and store the totalPeople in another variable.