Java While Statement Not Working

Hello,

I am a beginning java programmer that is working on an invoicing program. This program has a method that figures out what boxes to use to ship a product. Here are the requirements of this method:

30 products fit in a large box
20 products fit in a medium box
10 products fit in a small box

As of right now, I am trying to use while statements to add the total for each boxes. I'm not sure if this is the right approach or not, but hopefully, if it's not, you can guide me in the right direction.

When I click the create invoice button, this code executes along with the other needed methods to create the invoice.

Here is the code:

 
while (totalBags >= 30 && totalBags > 0)
		{
			 lgBoxes = lgBoxes + 1;
			 totalBags = totalBags - 30;
		}
		
		while (totalBags <= 20 && totalBags > 10)
		{
			medBoxes = medBoxes + 1;
			totalBags = totalBags - 20;
		}

		while (totalBags <= 10 && totalBags > 0)
		{
			smBoxes = smBoxes + 1;
			totalBags = totalBags - 10;	
		}

Open in new window


When I put in 315 products, the program should pick 10 large boxes, 1 medium, and 0 small boxes, but instead, it picks 10 large boxes, 0 medium, and 0 small.

What do I have to do to fix this issue?

Thank you in advance for your help!

Kody
LVL 6
Kody-BurgAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:

iT is not clear what you are doing - if you have a batch of bags coming to you
and you what to know into which box to packge it and you want to count total number
of boxes of each kind then
let's assume that your current batch contains totalBags

and up to now you used
smBoxes of small, medBoxes of medium and lgBoxes of large boxes, then
you rather use if... else construction than while in this case:

if(totaBags >20){
System.out.println("Too many bags: have no sucha big box")

} else if( totalBags > 10 && totalBags <=20){

lgBoxes = lgBoxes+1;

} else  if( totalBags > 5 && totalBags <=10){

medBoxes = medBoxes+1;

} else if(totalBags > 0 && totalBags <=5){
smBoxes = smBoxes+1;
} else {
System.out.println("error:  There cannot be negative number of Bags"
}


After you handle all your batches you'll count
total boxes as lgBoxes + medBoxes + smBoxes
You don't need to count them at every step


0
Kody-BurgAuthor Commented:
Sorry, it's hard to explain.

The objective is to always fill the boxes so that all boxes are filled to capacity, except possibly one
small box may not be entirely full, so that the boxes are effiently and cost effectively shipped to the customer.

I think I need to use the mod (%) operator and the divisor (/) operator to do it. I'm just not sure where to start.

0
for_yanCommented:
Ok now I see...
But you still don't need while, if -... else should be OK.





0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

for_yanCommented:


And you don't even need if ... else
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You code above actually does work, for the input you specified, 315, but there is still a problem, it doesn't work for an input of say 325.

You need something like the below...

 
while (totalBags >= 30)
		{
			 lgBoxes = lgBoxes + 1;
			 totalBags = totalBags - 30;
		}
		
		while (totalBags >= 20)
		{
			medBoxes = medBoxes + 1;
			totalBags = totalBags - 20;
		}

		while (totalBags > 0)
		{
			smBoxes = smBoxes + 1;
			totalBags = totalBags - 10;	
		}

Open in new window


But as you elude to, while loops aren't the best way to do this. Just use some maths!! Like below...

 
lgBoxes = (int) (totalBags / 30);
totalBags = totalBags % 30;

medBoxes = (int) (totalBags / 20);
totalBags = totalBags % 20;

smBoxes = (int) (totalBags / 10);
totalBags = totalBags % 30;

if(totalBags > 0)
  smBoxes = smBoxes + 1;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
for_yanCommented:

int N;
int LgBoxes
int medBoxes
int smBoxes

lgBoxes = N/20;

medBoxes = (N-(lgBoxes*20))/10;

if((smBoxes%5) = 0)
smBoxes = ((N-lgBoxes*20-medBoxes*10))/5;
else
smBoxes = (((N-lgBoxes*20-medBoxes*10))/5)+1;

totalBoxes = lgBoxes +medBoxes + smBoxes;


0
for_yanCommented:
corected:


int totalBags;
int LgBoxes
int medBoxes
int smBoxes

lgBoxes = totalBags/30;

medBoxes = (totalBags-(lgBoxes*30))/20;

if((smBoxes%10) = 0)
smBoxes = ((totalBafgs-lgBoxes*30-medBoxes*20))/10;
else
smBoxes = (((totalBags-lgBoxes*30-medBoxes*20))/10)+1;

totalBoxes = lgBoxes +medBoxes + smBoxes;

0
Kody-BurgAuthor Commented:
I can't believe I had forgotten about the MOD operator. I was in the middle of figuring it out on my own when you both posted your comments on how to do it.

Thank you for your help! It is much appreciated.

Kody
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Sorry, that last piece of code should have been this...

 
lgBoxes = (int) (totalBags / 30);
totalBags = totalBags % 30;

medBoxes = (int) (totalBags / 20);
totalBags = totalBags % 20;

smBoxes = (int) (totalBags / 10);
totalBags = totalBags % 10;

if(totalBags > 0)
  smBoxes = smBoxes + 1;

Open in new window


You can even get rid of the last else statement with some tricky maths, like this...

 
lgBoxes = (int) (totalBags / 30);
totalBags = totalBags % 30;

medBoxes = (int) (totalBags / 20);
totalBags = totalBags % 20;

smBoxes = (int) ((totalBags + 9) / 10);

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.