programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Loops to find best fit

Bam Berth
Greenhorn
Posts: 2
I need to be be able to find the best room to put a party of size n into given the capacity of rooms.

Basically I just need help writing a method that takes in the roomCapacity[] array and i and gives back the index at which the room capacity is most efficient with the size of the party.

Program should function somewhat like this (user input is bold and can be changed to anything positive).

How many rooms do you have?
3
What is the capacity of room 1?
30
What is the capacity of room 2?
20
What is the capacity of room 3?
10
How many parties are coming in?
3
How big is party 1?
11
The current best fit is room 2 with a capacity of 20
How big is party 2?
12
The current best fit is room 1 with a capacity of 30
How big is party 3?
3
The current best fit is room 3 with a capacity of 10

Any help or suggestions are appreciated, thanks!

Junilu Lacar
Sheriff
Posts: 11494
180
Welcome to the Ranch!

We don't give out solutions here. The help you can expect to receive here will be limited to tips and leading questions that are meant to get you to think about your problem and the solution. You're expected to do your own homework.

One of the things we advise most often is to stop typing, step away from the keyboard, get a pen/pencil and a piece of paper and work through the problem by hand.  As you work through the problem, try to describe the process of solving it in plain, simple English. Write that description down. A correct program solution usually reads just like how you describe it in plain English.

Liutauras Vilda
Sheriff
Posts: 4929
334
• 1
1. I'm interested, why have you decided to follow different indexing logic across those two loops? To confuse who? I think prefered is a bottom routine.

2. What should happen if party is as big as 31 person in it?

3. Have you covered objects? I find particularly wrong to model room's and parties capacities with arrays. That is something not intuitive and could mess up ones head.

Bam Berth
Greenhorn
Posts: 2
Liutauras Vilda wrote:1. I'm interested, why have you decided to follow different indexing logic across those two loops? To confuse who? I think prefered is a bottom routine.

2. What should happen if party is as big as 31 person in it?

3. Have you covered objects? I find particularly wrong to model room's and parties capacities with arrays. That is something not intuitive and could mess up ones head.

1.  I'm not exactly sure why I chose to do it that way...  I was getting errors doing it other ways and then did it that way and it worked.

2.  If 31 is entered then the program will say that there are no rooms that can hold a party of that size (not shown in the code).

3.  I haven't covered objects yet so arrays are the only thing I can work with.

Campbell Ritchie
Marshal
Posts: 56600
172
Bam Berth wrote:. . . 1.  I'm not exactly sure why I chose to do it that way...  I was getting errors doing it other ways and then did it that way and it worked.
What sort of errors were you getting? Was it printing 01 11 12 instead of 1 2 3? Read the Java® Language Specification for more details about that error. Before you do the apparent addition to get Fiddlers 3, you have created the String "Fiddlers 1" and you can't do arithmetic with the 1 any more.
Try System.out.print(...) instead of println in lines 9, 12 and 17.
. . . I haven't covered objects yet so arrays are the only thing I can work with.
What do they teach at schools nowadays?

And welcome again

Liutauras Vilda
Sheriff
Posts: 4929
334
Bam Berth wrote:1.  I'm not exactly sure why I chose to do it that way...
Oh, I see. Alright, so you had problems with rooms numbers, and in addition of that probably there were problem Campbell pointed out.