//Hello(apologize for any formatting errors)
//I am trying to finish this program that is supposed to act as a club bouncer. Only letting a max amount of people (125) in at one time. Naturally people leave throughout the night, so I was trying to //implement that with the random variable, but it keeps producing negative values. Any help would be greatly appreciated.

import java.util.Random;
     import javax.swing.JOptionPane;

       public class Clubmanger
    {
       public static void main(String[] args)
       {
          int counter = 0;
            Random leaving=new Random();
        do
        {
            int addperson=Integer.parseInt(JOptionPane.showInputDialog("Welcome to Club G, how many people in your party?"));
            if(addperson+counter>125)
            {
                JOptionPane.showMessageDialog(null,"There are too many people inside the club to allow you entry, please try again later");
                System.exit(0);
            }
            else
            {
                counter=+addperson;
                JOptionPane.showMessageDialog(null,"There are currently: "+counter+" people inside.");
            }
            int personleaving=leaving.nextInt(counter)+1;
            counter=-personleaving;
            JOptionPane.showMessageDialog(null,"There are:"+counter+"inside the club.");

        }while(counter<=124);    
    }

    }

Dani AI

Generated

Two small logic errors are causing the symptoms you see: the running total is being replaced instead of incremented, and the random leaving count isn’t constrained to the current occupancy. already pointed out that you shouldn’t negate the leaving value; was correct to call out the random bound. Fix both and the negative/out‑of‑range counts go away.

Key points to apply:

  • Always add a new party to the existing total (don’t overwrite the total).
  • Generate the number leaving with a bound based on the current occupancy. Remember that Random.nextInt(n) produces 0..n-1 and requires n>0; using nextInt(occupancy + 1) yields 0..occupancy safely.
  • Subtract the leaving count from the total (do the subtraction explicitly) and validate user input / handle cancelation so you never try nextInt(0).

A short, safe example (variable names changed from the original) that shows these ideas:

import java.util.Random;
import javax.swing.JOptionPane;

public class ClubBouncerExample {
    public static void main(String[] args) {
        final int MAX_CAP = 125;
        int occupancy = 0;
        Random rnd = new Random();

        while (true) {
            String s = JOptionPane.showInputDialog("How many in your party?");
            if (s == null) break; // user cancelled
            int group;
            try { group = Integer.parseInt(s.trim()); }
            catch (NumberFormatException ex) { JOptionPane.showMessageDialog(null, "Enter a number."); continue; }
            if (group <= 0) { JOptionPane.showMessageDialog(null, "Positive group size required."); continue; }
            if (occupancy + group > MAX_CAP) { JOptionPane.showMessageDialog(null, "Club full for now."); continue; }

            occupancy = occupancy + group; // add the party
            int leaving = rnd.nextInt(occupancy + 1); // 0..occupancy
            occupancy = occupancy - leaving; // remove those who left

            JOptionPane.showMessageDialog(null, "Now inside: " + occupancy);
        }
    }
}

Troubleshooting notes: guard against zero/negative input, handle cancel, and decide whether you want “0 allowed to leave” (realistic) or “at least one leaves” — if the latter, only compute a leave value when occupancy>0 and use a different bound.

Recommended Answers

All 3 Replies

that is because tour random variable has values greater than your counter, try to limit your randomization to the counter

but I want to limit it to only the values that the counter has. I don't want it to generate per say 80 if there are only 30 people inside

No your random variable is giving correct value.The error is because of line 24

  counter=-personleaving;//you are makin the value negative here by taking negative of personleaving

I think so you want to subtract personleaving from counter.It should be:-

 counter-=personleaving; //subtract personleaving from counter.
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.