• March 2nd, 2012, 08:32 AM
tommyabc
Code :

```Class SetZero{ int x,y; int[][] matrix = new int[200][200];   void setMartixZero(){ for(x=1 ; x<=200; x++) for(y=1; y<=200; y++) matrix[x][y] = 5; }   }```
There is an error in line 8. Can anyone help me? Thank you.
• March 2nd, 2012, 08:54 AM
andreas90
I'm not sure what you are trying to do but
Code Java:

` matrix[x][y]`
is a location in your two-dimensional int array, so you need to assign it an int value. For example
Code Java:

`matrix[x][y] = 13;`
• March 2nd, 2012, 09:22 AM
tommyabc
I want to set matrix[0][0]=5, matrix[0][1]=5 ... matrix[200][200] = 5
I want to use for loop to do so but it needs to work in a method.
Outside a method, I can't use for loop so i need to write a lot for my purpose.
• March 2nd, 2012, 09:41 AM
andreas90
Can you explain the problem you are facing now? You have the "setMartixZero()" method which set all the matrix[][] locations equal to 5. You can just call it when you want.
• March 2nd, 2012, 09:48 AM
Mr.777
If you are facing error/exception, paste here the full text so that we could have a look.
Also, i can assume there must be index out of bound exception. Coz, you are trying to save the value 5 at matrix[200][200] as the total size of your array is 200X200 which means matrix[199][199] will be the last row and column of your array.
• March 2nd, 2012, 10:17 AM
tommyabc
I made a careless mistake here.
My code is fine now but I want to ask how i can set matrix[0][0]=5, matrix[0][1]=5 ... matrix[200][200] = 5
by using loop? A loop can only be performed inside a method.
• March 2nd, 2012, 10:42 AM
Mr.777
Quote:

A loop can only be performed inside a method.
Can you provide me with the reference material where you've studied that a loop can not perform inside a method???? Loop can be placed anywhere in your code. Place it in main() and don't make a separate method for initializing the array(if only you intended to do this).
Quote:

how i can set matrix[0][0]=5, matrix[0][1]=5 ... matrix[200][200] = 5
If you want to use matrix[200][200], don't forget to change the size of array to matrix[201][201] or face the exception.
• March 3rd, 2012, 09:45 PM
tommyabc
Quote:

Originally Posted by Mr.777
Can you provide me with the reference material where you've studied that a loop can not perform inside a method???? Loop can be placed anywhere in your code. Place it in main() and don't make a separate method for initializing the array(if only you intended to do this).

If you want to use matrix[200][200], don't forget to change the size of array to matrix[201][201] or face the exception.

Sorry, are there any misunderstanding? I mean that loop can only be placed inside a method. Outside a method, there is an error. So what i should do if I want to declare
matrix[0][0] = 5, matrix[0][1] = 5, ... , matrix[200][200] = 5.
Code :

```Class Matrix{ int x,y; int[][] matrix = new int[201][201]; for(x=1 ; x<=200; x++) //there will be an error because of outside the method for(y=1; y<=200; y++) matrix[x][y] = 5; }```
Thank you.
• March 3rd, 2012, 09:56 PM
tommyabc
Quote:

Originally Posted by andreas90
Can you explain the problem you are facing now? You have the "setMartixZero()" method which set all the matrix[][] locations equal to 5. You can just call it when you want.

Do you mean that I can only set all the matrix[][] locations equal to 5 with a method?
Because I want to do the following thing but it seems so stupid.
Code :

` static int[][] matrix = {{5 /* x200 times */ }};`
Thank you.
• March 4th, 2012, 05:17 AM
Mr.777
You can never write loops inside the class like this. You need to have a main() method to start your program. In the main(), use the loop and do whatever you want to do. Like;
Code :

```class A{ public int x; public static void main(String[] args){ x = 5; } }```
• March 4th, 2012, 07:22 AM
tommyabc
Quote:

Originally Posted by Mr.777
You can never write loops inside the class like this. You need to have a main() method to start your program. In the main(), use the loop and do whatever you want to do. Like;
Code :

```class A{ public int x; public static void main(String[] args){ x = 5; } }```

But I need to initiallise matrix[0][0]=5, matrix[0][1]=5 ... matrix[200][200] = 5 in a class which does not have main class. May i know that if there are any ways i can do it other than using a method.Thank you
• March 4th, 2012, 07:54 AM
snowguy13
No, you need to use a method. But it isn't that complicated. When you typed this code:

Code Java:

``` for(x=1 ; x<=200; x++) for(y=1; y<=200; y++) matrix[x][y] = 5;```

it was very close to perfect. The one thing you must remember is that arrays start at index 0 (not at index 1).
• March 4th, 2012, 08:34 AM
andreas90
Quote:

Originally Posted by tommyabc
Do you mean that I can only set all the matrix[][] locations equal to 5 with a method?
Because I want to do the following thing but it seems so stupid.
Code :

` static int[][] matrix = {{5 /* x200 times */ }};`
Thank you.

The for loops are setting ALL the indexes of your int array equal to 5. This what you want to do as far as i understand.
Quote:

Originally Posted by tommyabc
But I need to initiallise matrix[0][0]=5, matrix[0][1]=5 ... matrix[200][200] = 5 in a class which does not have main class. May i know that if there are any ways i can do it other than using a method.Thank you

You can put the for loops in a separate method which you can call when you want to initialise "matrix[][]".
And as Mr.777 and snowguy13 have mentioned be careful with your array indexes (200 indexes are from 0 to 199).
• March 4th, 2012, 05:50 PM
tommyabc
Quote:

Originally Posted by snowguy13
No, you need to use a method. But it isn't that complicated. When you typed this code:

Code Java:

``` for(x=1 ; x<=200; x++) for(y=1; y<=200; y++) matrix[x][y] = 5;```

it was very close to perfect. The one thing you must remember is that arrays start at index 0 (not at index 1).

Then i will use a method to do it. Thank you.
• March 5th, 2012, 04:55 AM
Mr.777