//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();
            int addperson=Integer.parseInt(JOptionPane.showInputDialog("Welcome to Club G, how many people in your party?"));
                JOptionPane.showMessageDialog(null,"There are too many people inside the club to allow you entry, please try again later");
                JOptionPane.showMessageDialog(null,"There are currently: "+counter+" people inside.");
            int personleaving=leaving.nextInt(counter)+1;
            JOptionPane.showMessageDialog(null,"There are:"+counter+"inside the club.");



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, learning, and sharing knowledge.