J M
J M

Reputation: 369

saving user input into an array and making sure it does not violate index

I am working on an assignment and editing a program. I am asking the user to enter a their salesperson number, product number, and how much they sold. I am trying to save the sales data as an array called sales. However, I cannot properly access elements of two dimensional array.

The array is defined as:

double[][] sales = new double[ 5 ][ 4 ] 

but when I try to do this:

sales[ product - 1 ][ person - 1 ] += amount;

... it doesn't save increment the sales amount. I think I am violating the index of the array.

Here is the entire code block:

import java.util.Scanner;

public class Sales2
{
   public static void main( String[] args )
   {
      Scanner input = new Scanner( System.in );
      // sales array holds data on number of each product sold
      // by each salesperson
      double[][] sales = new double[ 5 ][ 4 ]; // 5 salespeople, 
                                               //4 products each person

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

      while (person != -1)
      {
      System.out.print( "Enter product number: " );
      int product = input.nextInt(); // the product index
       // prompt user to enter product number and save it as an integer
      System.out.print( "Enter sales amount: " );
      double sales = input.nextInt();
    // promp to enter sales amont and save it as double

      sales[ product - 1 ][ person - 1 ] += amount;
         // Having trouble with the following.  I tried to manipulate
                 // the above array but nothing will work.  thanks
                 // error-check the input number for the array boundary
         // that is the person index should be 0 - 3
         // and the product index should be 0 - 4
         // notice that array index start with 0  
         // save the input to the sales 
                //array like sales[ product - 1 ][ person - 1 ] += amount;
         // or print message for the out of boundary input 


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


      // total for each salesperson
      double[] salesPersonTotal = new double[ 4 ];

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

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

      // To do -
      // for each column of each row, print the appropriate
      // value representing a person's sales of a product
      // and calculate and print out the total for each product


      System.out.printf( "%25s","1", "2", "3",
            "4", "5" );

      // To do -
      // print out for each sales person total
          // I have been messing with these numbers but
            //it doesnt seem to be working.
   } // end main
} // end class Sales2

Upvotes: 0

Views: 2410

Answers (2)

Danyun Liu
Danyun Liu

Reputation: 3092

If you worry about the violating the index, you can try to use Map to store your date. Such as:

Map<Integer,Map<Integer,Integer>> saleMap= new HashMap<Integer,Map<Integer,Integer>>;
Map<Integer,Integer> productMountMap = new HashMap<Integer,Integer>;
productMountMap.put(product,mount);
//.. add more proudct mount map as you like. you don't need worry about the violate index
saleMap.put(person,productMountMap);

//Get one person's data using "person" as the key.
Map<Integer,Integer> b = saleMap.get(person) // so you get the product mount map.

Upvotes: 0

TechZen
TechZen

Reputation: 64428

Okay, my Java is extremely rusty (I just drove by to edit) but even I can see that:

You use the symbol sales here:

double[][] sales = new double[ 5 ][ 4 ];

... but then you use the exact same symbol here:

double sales = input.nextInt();

... which is bad practice in any language. It will confuse humans even if not the VM.

I suspect your problem with this line:

sales[ product - 1 ][ person - 1 ] += amount;

... is that, even if the VM does figure out which of the two sales you intended, well, you never defined the symbol amount to mean anything. I think what you wanted was actually:

  double amount = input.nextInt();
  sales[ product - 1 ][ person - 1 ] += amount;

This kind of thing is easy to miss if you look at the same code over and over again. You begin to see what you intended to type as well as the associated logic instead of what the code actually reads. Everybody does it, even old hands.

Upvotes: 3

Related Questions