Results 1 to 3 of 3
  1. #1
    CJ1031 is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default 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];
    }
    }

    return totalSales;
    }

    }



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

  2. #2
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    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. #3
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    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.

Similar Threads

  1. Storing Inputs in Two Linked Arrays
    By essence388 in forum New To Java
    Replies: 1
    Last Post: 12-26-2010, 02:21 PM
  2. Arrays and use of null or empty inputs
    By Desmond in forum New To Java
    Replies: 6
    Last Post: 07-27-2010, 04:54 PM
  3. Arrays and Null inputs
    By Desmond in forum New To Java
    Replies: 4
    Last Post: 07-21-2010, 06:23 AM
  4. Someone plz help... how to block inputs
    By waklo99 in forum New To Java
    Replies: 4
    Last Post: 03-15-2010, 06:44 AM
  5. How to create this if many inputs?
    By sarahannel123 in forum New To Java
    Replies: 3
    Last Post: 05-18-2008, 04:22 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •