Link to home
Start Free TrialLog in
Avatar of Kody-Burg
Kody-BurgFlag for United States of America

asked on

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
Avatar of for_yan
for_yan
Flag of United States of America image


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


Avatar of Kody-Burg

ASKER

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.

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







And you don't even need if ... else
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;

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
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