Here is my assignment.
Ask the user for the number of rows (or aisles of coffee bins). Then prompt the user for the number of bins in each aisle. Create a two dimensional array to correspond to the numbers that the user entered. The array will hold numbers representing pounds. Now prompt the user for the pounds in each bin. Once all the bins are filled, display the each element in the array (in a good format) and display the total number of pounds in the coffee shop.
The program compiles but it isn't working because the array is out of bounds. Am I totally off base, or do I just need to rework what I have?
public class CoffeeArray
public static void main( String args )
Scanner input = new Scanner( System.in );
int bins = 0;
int totalWeight = 0;
System.out.print( "Please input the number of aisles\n" );
aisles = input.nextInt();
coffee = new int [aisles];
if ( counter = 0; coffee.length > counter; counter++)
number = counter + 1;
System.out.printf( "\nHow many bins are in each aisle %d?\n" , number );
bins = input.nextInt();
coffee[aisles] = bins;
System.out.print( "How much does each coffee bin weigh?\n" );
weight = input.nextInt();
total = new int [aisles];
total[aisles] = bins * weight;
System.out.printf( "%s%10s%10s\n", "Aisles" , "Bins" , "Weight" );
System.out.printf( "%d%10d%10d\n" , coffee[aisles], coffee[aisles], total[aisles]);
totalWeight += total;
System.out.printf( "The total amount of pounds is: %d" , totalWeight );
You can rework what you have, with a little more new code added in.
you need to ask for the number of aisles, ask for the number bins, _then_ construct your 2D array.
I think that the question is assuming that each aisle has the same number of bins.
You need to add in a nested for loop to go through and ask the user to input the ammount of coffee in each bin.
While they're doing that, you could also add up the totals too.
I wish I understood what you're saying. I think my brain is fried for tonight.
Ask how many aisles
Ask how many bins per aisle
make 2D array from those numbers
For each aisle, ask how much coffee is in each bin
Keep a running total of the amount of coffee
Print (nicely) to screen the amount of coffee in each bin
Print the total amount of coffee to screen.
It was because I'd been working on this and other things all day yesterday. I'm awake from the new morning and understand your logic now. Thank you. I'll see if I can code this.
Last Post: 03-01-2007, 07:23 PM
Last Post: 10-31-2006, 05:51 PM
Last Post: 05-24-2006, 04:27 AM
By premartha in forum C++
Last Post: 10-21-2005, 11:06 AM
By sparkette in forum Java
Last Post: 07-02-2005, 01:35 PM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL