There is an error in line 8. Can anyone help me? Thank you.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; } }
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
Members have full access to the forums. Advertisements are removed for registered users.
There is an error in line 8. Can anyone help me? Thank you.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; } }
Last edited by tommyabc; March 2nd, 2012 at 11:15 AM. Reason: careless mistake for x=0 and y=0 in the loop
I'm not sure what you are trying to do butis a location in your two-dimensional int array, so you need to assign it an int value. For examplematrix[x][y]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.
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).A loop can only be performed inside a method.
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.how i can set matrix[0][0]=5, matrix[0][1]=5 ... matrix[200][200] = 5
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.
Thank you.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; }
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; } }
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).
Use highlight tags to help others help you!
[highlight=Java]Your prettily formatted code goes here[/highlight]
Using these tags makes your code formatted, and helps everyone answer your questions more easily!
Wanna hear something funny?
Me too.
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.
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).
Or you can use a constructor instead.