Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 22nd, 2007, 10:18 AM
Authorized User
 
Join Date: May 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default two-dimensional array

Hi...i have been having grate difficulty figuring this question out..if you have any info that might help..it will be grately appreciated.:(

Use a two-dimensional array to solve the follwoing problem: A company has four salespeople(1 to 4) who sell five different products (1 - 5). Once a day, each salesperson passes in a slip for each type of product sold. Eash slip contains the following:
a) The Salesperson number
b) The product number
c) The total dolar value of the product sold that day.

Thus, each salesperson passes in between 0 - 5 sales slips per day. Assume that the information from all the slips for last month is available. Write an application that will read all this information for last month's sales and summarize the total sales by salesperson and by product. All totals shouldbe stored in the tow-dimensional array sales. After processing all the information for last months, display the results in tabular format, with each column representing a particulat salesperson and each row representing a particular product. Cross-total each row to get the total sales of each product for last months. Cross-total each column to get the total sales by salesperson for last month. Your tabular output should include thes cross-totals to the right of the totaled rows and to the bottom of the totaled columns.

 
Old May 22nd, 2007, 12:11 PM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi,

To have one days data using two dimensional array can be done as follows,

Create a class which will hold the three required values sales person number , product number, and product value.

Object[][] sales = new Object[4][5]();

for each slip create an object for the above defined class and store it in the perticular position, position depends on the sales person id and his/her slip number, i.e. if he/she has passed two slips and the current one is third the the second index is 2, if his/her id is 2 then the first index id 1 you can store the newly created value in sales[1][2] position.

BUT I STROGLY RECOMMEND TO USE JAVA COLLECTIONS instead of using plain arrays.

Regards,
Rakesh
 
Old May 23rd, 2007, 04:08 AM
Authorized User
 
Join Date: May 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Can you give me any more code..im still battling...

 
Old May 23rd, 2007, 07:03 AM
Authorized User
 
Join Date: May 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is code for a specific program that I have to write but I am getting the following error message when I compile.

Please help?


// Q7.20 Two dimensional array: Sales2.java
// Total sales for sales persons
import java.util.Scanner;
public class Sales2
{
    public void calculateSales()
    {
        Scanner input = new Scanner(System.in);
        //Sales array data on products sold by salesman
        double sales[][] = new double[5][4];

        System.out.print("Enter sales person number (-1 to end):");
        int person = input.nextInt();

        while (person != -1)
        {
            System.out.print("Enter product number:");
            int product = input.nextInt();
            System.out.print("Enter sales amont:");
            double amount = input.nextDouble();

        //error check input
        if (person >= 1 && person < 5 && product >=1 && product < 6 &&
            amount >= 0)
            sales[product -1][person -1] =+ amount;
       else
            System.out.println("Invalid Input!");

       System.out.print("Enter sales person number(-1 to end):");
       person = input.nextInt();
       }//end while


        //salesperson totals
        double salesPersonTotal[] = new double[4];

        //display data table
        for (int column = 0; column < 4; column ++)
            salesPersonTotal[column] = 0;

        System.out.printf("%8s%14s%14s%14s%14s%10s\n",
            "Product", "Salesperson 1", "Salesperson 2","Salesperson 3",
            "Salesperson 4", "Total");


       //Printing a person's sales of a product
       for (int row = 0; row < 5; row ++)
       {
        double productTotal = 0.0;
        System.out.printf("%8d%",(row +1) );

        for ( int column = 0; column < 4; column ++){
            System.out.printf("%14.2f", sales[row][column]);
            productTotal += sales[row][column];
            salesPersonTotal[column] += sales[row][column];
            } //end for loop


        System.out.printf("8%s", "Total");

        for (int colum = 0; column < 4; column ++)
            System.out.printf("14.2%f", salesPersonTotal[column]);

       System.out.println();
    }// end calculations
}// end class sales



// Q7.20 Two dimensional array: Sales2Test.java
//Test application for sales class

public class Sales2Test
{
    public static void main(String args[])
    {
        Sales2 application = new Sales2();
        application.calculateSales();
__
    }//end main
}




But it gives an error message like below? Why? And how can I fix it?



Sales2.java:81: '}' expected
  }
  ^
error


 
Old May 23rd, 2007, 09:35 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi,

You forgot to close the following for loop


       //Printing a person's sales of a product
       for (int row = 0; row < 5; row ++)
       {


Regards,
Rakesh





Similar Threads
Thread Thread Starter Forum Replies Last Post
using 2 dimensional array Abraham EJB 0 July 17th, 2007 09:27 AM
Sorting 2 dimensional array rgalehouse Javascript How-To 2 December 2nd, 2006 10:46 AM
fill two-dimensional array tobian Excel VBA 0 February 21st, 2006 05:55 PM
2 dimensional array trangd Beginning PHP 0 August 18th, 2005 12:37 AM
2 dimensional array venterjo General .NET 2 January 23rd, 2005 09:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.