Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Anyone know what this code is doing?

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Anyone know what this code is doing?

    In an exercise in my book, I've come across an example that doesn't go too far into explaining a section of the code. I don't know what it's called so I'm having a tough time finding it. Here's a clip of it, what I've highlighted in bold and underlined is what's got me stumped...

    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 ];
     
          System.out.print( "Enter salesperson 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 amount: " );
             double amount = input.nextDouble();
     
             // error-check the input
             if ( person >= 1 && person < 5 &&
                   product >= 1 && product < 6 && amount >= 0 )
               [B][U][SIZE="3"] sales[ product - 1 ][ person - 1 ] += amount;[/SIZE][/U][/B]
             else
                System.out.println( "Invalid input!" );
     
             System.out.print( "Enter salesperson number (-1 to end): " );
             person = input.nextInt();
          }

    Any idea what "sales[ product - 1 ][ person - 1 ] += amount;" is doing? Because I don't, I'm stumped. Anyone know what it is called at the very least?


  2. #2
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Anyone know what this code is doing?

    Wow, I'm dumb, Java = Zero based, so sales[ product -1] [ person -1 ] simply sets the value entered to the correct element in the array... Right?

    I think that's right at least.

  3. #3
    Member
    Join Date
    Feb 2010
    Location
    Auburn, AL
    Posts
    31
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: Anyone know what this code is doing?

    You're right, of course. The people and products are indexed from 1 to 4, and the products 1 to 5, so subtracting by 1 allows these indices to be used for the arrays which are indexed from 0 to 3 and from 0 to 4, respectively.
    Let me know what you think of my website:
    http://www.auburn.edu/~carpept
    Comments or suggestions are appreciated!