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?
 
mccarlConnect With a Mentor IT 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
 
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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





0
 
for_yanCommented:


And you don't even need if ... else
0
 
for_yanConnect With a Mentor Commented:

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.