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


i working on assignment , editing program. asking user enter salesperson number, product number, , how sold. trying save sales data array called sales. however, cannot access elements of 2 dimensional array.

the array defined as:

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

but when try this:

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

... doesn't save increment sales amount. think violating index of array.

here 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       // each salesperson       double[][] sales = new double[ 5 ][ 4 ]; // 5 salespeople,                                                 //4 products each person        system.out.print( "enter salesperson number (-1 end): " );       int person = input.nextint(); // salesperson index        while (person != -1)       {       system.out.print( "enter product number: " );       int product = input.nextint(); // product index        // prompt user enter product number , save integer       system.out.print( "enter sales amount: " );       double sales = input.nextint();     // promp enter sales amont , save double        sales[ product - 1 ][ person - 1 ] += amount;          // having trouble following.  tried manipulate                  // above array nothing work.                   // error-check input number array boundary          // person index should 0 - 3          // , product index should 0 - 4          // notice array index start 0            // save input sales                  //array sales[ product - 1 ][ person - 1 ] += amount;          // or print message out of boundary input             system.out.print( "enter salesperson number (-1 end): " );          person = input.nextint(); // input next sales person       } // end while         // total each salesperson       double[] salespersontotal = new double[ 4 ];        // display table             ( int column = 0; column < 4; column++ )          salespersontotal[ column ] = 0;  // initialize array        system.out.printf( "%8s%14s%14s%14s%14s%10s\n",             "product", "salesperson 1", "salesperson 2",             "salesperson 3", "salesperson 4", "total" );        // -       // each column of each row, print appropriate       // value representing person's sales of product       // , calculate , print out total each product         system.out.printf( "%25s","1", "2", "3",             "4", "5" );        // -       // print out each sales person total           // have been messing these numbers             //it doesnt seem working.    } // end main } // end class sales2 

okay, java extremely rusty (i drove edit) can see that:

you use symbol sales here:

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

... use exact same symbol here:

double sales = input.nextint(); 

... bad practice in language. confuse humans if not vm.

i suspect problem line:

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

... that, if vm figure out of 2 sales intended, well, never defined symbol amount mean anything. think wanted actually:

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

this kind of thing easy miss if @ same code on , on again. begin see intended type associated logic instead of code reads. it, old hands.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -