Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6128249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:36:20+00:00 2026-05-23T16:36:20+00:00

I am working on an assignment and editing a program. I am asking the

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:36:21+00:00Added an answer on May 23, 2026 at 4:36 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an assignment to write a program mirroring ac(1) . The output
I'm working on an assignment where the program needs to calculate and display the
I am working on an assignment, and I need to enter an SQL Query
I'm working on an assignment that allows the user to display events that are
I'm working on an assignment for school, and am trying something beyond for extra
I'm working on assignment where the professor is asking for a solution to solve
I'm working on an assignment that involves writing a program to process data (calculate
I'm working on an assignment that is telling me to assume that I have
I am working on an assignment for networking where we are supposed to create
I am working on an assignment where one of the problems asks to derive

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.