# Thread: Help with 2-D Arrays and Inputs

1. Member
Join Date
Apr 2011
Posts
1
Rep Power
0

## Help with 2-D Arrays and Inputs

I am having trouble with my assignment and got totally lost with 2-D array. I'm not that good with java, but still trying my best to learn it for class. I have to have users input their answers in this problem. Here is the question:

Requirements: Write a program to allow the user to input sales data for several sales representatives for each of the four quarters of the year (you can refer to them as Q1, Q2, Q3, and Q4), and then print some summary data. You will need an array of Strings for the names, and a two-dimensional array of ints for the sales data. For your input, start by prompting for the number of sales representatives. Use a loop to get the rest of your input. For each representative, prompt for his or her name, and then use another loop to get that representative's sales amount (in dollars) for each quarter. After all input is completed, print the total sales for each quarter and for the entire year, as well as the average sales per representative for each quarter and the entire year.

This is all that I can come up with so far:

import java.util.Scanner;

public class QuarterlySales
{
public static void main(String[] args)
{
int rows;
final int COLS = 4;
Scanner reps = new Scanner(System.in);
System.out.println("How many sales representatives do you have?");
rows = reps.nextInt();
int [][] arrayOfSets = new int [rows] [COLS];

Scanner in = new Scanner(System.in);
System.out.println("Enter the name of sales representative ");
String repNames = in.next();

int total = computeTotalSales(arrayOfSets);

System.out.println("The total sales for "
+ "the 4 quarters is: " + total);

printMonthlySales(repNames, arrayOfSets);

}

public static void printQuarterlySales(String[] repNames,
int[][] arrayOfSets)
{
System.out.println("Name Q1 Q2 Q3 Q4");

for (int i = 0; i < repNames.length; i++)
{
System.out.print(repNames[i] + " ");
for (int j = 0; j < arrayOfSets[i].length; j++)
{
System.out.print(arrayOfSets[i][j] + " ");
}
System.out.println();
}

}

public static int computeTotalSales(int[][] rows)
{
int totalSales = 0;
for (int i = 0; i < rows.length; i++)
{
for (int j = 0; j < rows[i].length; j++)
{
totalSales += rows[i][j];
}
}

}

}

If anyone can help with this, I really appreciate it. Thank you :)

2. Senior Member
Join Date
Aug 2008
Location
Stockholm, Sweden
Posts
119
Rep Power
0
You left out any actual questions you might have. All I see is a copy/paste of your homework and your attempt at it.

Does the code compile? If it does, what's the output? Does it differ from what you expect? If it doesn't, post the the exact error messages and what lines then indicate.

Also: Post code in PHP or CODE tags, no one's going to read that bold unindented mess.

3. Member
Join Date
Apr 2011
Location
Posts
30
Rep Power
0
What the "design" required in your assignment? I dont see a use for a 2D array at all, unless its something like

int[salesAmountPerQuarter][salesreps] salesData = new int[4][loopThroughAsManyRepsAsGiven];

The above is pseudocode as well. Also I cant run your code as you didnt post your
printMonthlySales(repNames, arrayOfSets); method.