How to call a method once everyday?

How to call an array method I wrote from another class:?

  • This is the array method I wrote to assign random numbers to an array: public SpreadSheet(int r, int c) { r = rows; c = cols; int [][] sheet= new int [r][c]; for (r=0; r < rows; r++) { for (c=0; c < cols; c++) { sheet[r][c] = (int)(100*Math.random()); }//end of loop for c }//end of loop for r } This is how I am trying to call it in another class with (5,7) 5 rows 7 columns: SpreadSheet whichSheet = new SpreadSheet(5,7);

  • Answer:

    You shouldn't set r and c equals to rows and cols. You pass 5 and 7 into the method; now r = 5, c = 7. And then in your method, you set r and c equals to another variable, r and c no longer contains 5 and 7, respectively. Second, the variables rows and cols never declared. Declare the 2 variables and than set rows and cols equals to r and c, respectively. public SpreadSheet(int r, int c) { int rows = r; int cols = c; int[][] sheet = new int[rows][cols]; for (int ROWS = 0; ROWS < rows; ROWS++) { for (int COLS = 0; COLS < cols; COLS++) { sheet[ROWS][COLS] = (int)(100*Math.random()); } } } And then you call the method in another class passing 5 and 7 into the method's parameter.

cory at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Where are the variables rows and cols defined? You are passing in rows and columns as parameters. Use them. I would rename the parameters from r and co to rows and cols. public SpreadSheet(int rows, int cols) Then, you need loop variables, use r and c for those: int r, c; Declare the array using the parameters, not the local variables: int [][] sheet= new int [rows][cols];

Ratchetr

the method named "SpreadSheet" is obviously a constructor, and it appears your call to it is in order, assuming it compiles.

halrosser

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.