1

Once again I am racking my brain as the error messages
keep coming. I am learning how to work with arrays - both regular and multi-dimensioned. I am having problems a) filling the array with the sales data and also with a section where I get the "cannot convert String to an int". When I modify this array to have a string value, - I then get a flip flopped error,- "cannot convert int to String. Any assistance is appreciated. Thanks.

    public class Sales{

       public static void main (String []Args)
       {   

        //a single dimension array containing the following customer names

       String [] Names = {"John Doe","Pete BuysAlot","Joe Stingy","Mary       LikesOurStuff" } ;
       //  for(int 0;i<Names.length; i++)            
          // System.out.printl=n(Names[i]);}
       //a single dimension array containing the names of  //each                   month               
       String[]Months= new String [11];

           Months[0] = "   Jan   ";
           Months[1] = "   Feb   ";
           Months[2] = "   Mar   ";
           Months[3] = "   Apr   ";
           Months[4] = "   May   ";  
           Months[5] ="    June  ";
           Months[6] ="    July  ";
           Months[7] ="    Aug   ";
           Months[8] ="    Sept  ";
           Months[9] ="    Oct   ";
           Months[10]="    Nov   ";
           Months[11]="    Dec   "; 




         // this next section creates the variables and data to create and initialize          
        //  a two dimension array that reflects the purchases each person made per month.
       //It will have the initial data in the following table

     int[][]slsData = { {200,50,30,300,155,220,80,70,95,110,3,400},
                 { 1200,2000,1500,900,1300,800,750,500,900,1200,1500,2000},
                      {10,0,0,20,5,30,10,0,0,10,15,0},
                      {500,100,200,400,600,200,150,155,220,336,43 ,455}
                               };

           String [][] slsTablePP = slsData[3][Months]; //here is where an  error occurance is. [months] is a declared as a new string array but errors.
           {
              for (int row = 0; row <Names.length; row++)
                 for (int col = 0;  col<Months.length; col++)
                 System.out.println(slsTablePP[row][col]);    }   


           // array to hold sales figures totals by  month 
              for( int x=0;x<mthlySales-1;x++)
             System.out.println(Names[i] + mthlySls[x]);
         }
     }
 } 
Dandelion
  • 744
  • 2
  • 13
  • 34
TimC
  • 13
  • 4

4 Answers4

0
slsData[3][Months]

Months should be an integer here, to access the nth element of the 3rd array in slsData. But the type of Months is String[], so that doesn't make sense. Maybe what you want to pass is the length of the Months array. In that case, you would use slsData[3][Months.length].

But since slsData is an array of array of ints, slsData[3][12] that would be an int. So you can't assign it to a variable of type String[][].

Note that you should respect Java naming conventions. Variables should have meaningful names, made of words (what do slsData and slsTablePP mean?), and should start with a lowercase letter (i.e. months, not Months).

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0
 String [][] slsTablePP = slsData[3][Months]; 

Months is not an integer. You cannot directly convert an int array to a String array. To convert an integer array to a string array take a look at Converting an int array to a String array

String[]Months= new String [11];

The size of the above array should be 12.

Community
  • 1
  • 1
AAB
  • 1,594
  • 2
  • 25
  • 40
  • Thank all of you very much for the time you spent helping me with this. I appreciate you sharing your expertise with me - I will definitely study the edits suggested and have paid close attention to the code changes. – TimC Apr 05 '15 at 22:53
0

Months is simply the name of your String array. In order to access individual values within the array you have to pass it the index of the value you want using the array operator [].

For instance:

String[] stringArray = new String[4];
String fourthString = stringArray[3];

or

String[] stringArray = new String[someList.size()]
for (int i=0; i<stringArray.length; i++) {
   stringArray[i] = someList.get(i);
   //some other stuff maybe
}
0

Obviously, this line is wrong:

String [][] slsTablePP = slsData[3][Months];

Arrays are typed and you cannot magically convert int[][] to String[][], furthermore, in Java, arrays are always indexed by int. This slsData[3][Months] where Months is of type String[][] has no meaning.
Note, in some language, like Python, you have associative arrays (Map and derived classes in Java) that can have an array syntax with string as index, e.g.

# python syntax
salesByMonth = {}
salesByMonth["jan"] = 2000;
salesByMonth["feb"] = 500; 

You have another issue when you declare the Months array. In

String[] Months= new String [11]; // <= should be 12 !

The value [11] is the size of the array, not the last index, hence it should be 12. In Java arrays are 0 indexed.

Below I put a commented example of how you can match your different data arrays.

public class Sales{
    public static void main (String []Args) {     
        //a single dimension array containing the following customer names
        String [] Names = {"John Doe","Pete BuysAlot","Joe Stingy","Mary LikesOurStuff" } ;
        //a single dimension array containing the names of each month               
        String[]Months= new String [12]; // <= 12 is the size not the last index !
        Months[0] = "   Jan   "; Months[1] = "   Feb   ";
        Months[2] = "   Mar   "; Months[3] = "   Apr   ";
        Months[4] = "   May   "; Months[5] = "   June  ";
        Months[6] = "   July  "; Months[7] = "   Aug   ";
        Months[8] = "   Sept  "; Months[9] = "   Oct   ";
        Months[10]= "   Nov   "; Months[11]= "   Dec   "; 

         // two dimension array that reflects the purchases each person made per month.
        int[][]slsData = { 
                { 200,   50,   30, 300,  155, 220,  80,  70,  95,  110,   3,   400},
                {1200, 2000, 1500, 900, 1300, 800, 750, 500, 900, 1200, 1500, 2000},
                {  10,    0,    0,  20,    5,  30,  10,   0,   0,   10,   15,    0},
                { 500,  100,  200, 400,  600, 200, 150, 155, 220,  336,  43 ,  455}
        };

        // match the sales data with name & month through the _indexes_ 
        // in the arrays. Here iName [0, 3] and iMonth [0, 11] will
        // give the sales amount for a (name, month): slsData[iName][iMonth]
        for (int iName = 0; iName < Names.length; iName++) {
            System.out.println("\nSales for "+Names[iName]);
            int total = 0; // total (re)set to 0 for each name
            for (int iMonth = 0; iMonth < Months.length; iMonth++) {
                System.out.println(Months[iMonth] + slsData[iName][iMonth]);
                total += slsData[iName][iMonth];
            }
            System.out.println("Total sales for "+Names[iName] + ": "+total);
        }
    }
} 
T.Gounelle
  • 5,953
  • 1
  • 22
  • 32