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]);
         }
     }
 } 
4
  • your array should be 12 index not 11. Commented Apr 4, 2015 at 7:00
  • What do you intend to happen with String [][] slsTablePP = slsData[3][Months] ? Commented Apr 4, 2015 at 7:04
  • You have to try with String.valueOf(int) method and use array as String array. Commented Apr 4, 2015 at 7:23
  • Hi Ravi.... I was was hoping that slsTablePP would be populated by the data in slsData. My understanding is that my code for slsTablePP just creates the table in an array format but does not fill it.?? Commented Apr 4, 2015 at 7:24

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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).

1 Comment

but what about String [][] slsTablePP = slsData[3][11]; does it work.I dont think?
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
}

Comments

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);
        }
    }
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.