Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

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

I'm not sure what you are trying to do but
` matrix[x][y]`
is a location in your two-dimensional int array, so you need to assign it an int value. For example
`matrix[x][y] = 13;`

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.

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.

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.

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.

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

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

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.
` static int[][] matrix = {{5 /* x200 times */ }};`
Thank you.

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;
```class A{
public int x;
public static void main(String[] args){
x = 5;
}
}```

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;
```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

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

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

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

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

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