Manoj Kumar Jain wrote:Just take out the "field" variable declaration out of the method like this
now you have to call createField() method to initilize this variable. I am declaring the "field" variable as static because all methods are static so you need not to make many changes. Just take out the "field" out and declare in class itself.
now you can call the methods by class name and dot operator
Campbell Ritchie wrote:You are perpetuating the use of static members. Why? You should always think, “why am i writing static?” before writing static.
Manoj Kumar Jain wrote:Hey Bruno,
I can see some of the problems in your code
Field is local to the method createField, so not available outside the method itself. You need to declare array in class as instance variable. You are not calling createField method anywhere then how this array can get initialized even if you declare it as instance variable. Array is having 7 columns so index will varies from 0-6 so, field[0][7] = 2; is invalid.
Jesper de Jong wrote:Welcome to the Ranch!
In your first piece of code, in the createField method, you are declaring a variable (line 6) that is an array of arrays of ints. But it is a local variable inside the method. Local variables exist only inside the method. So as soon as the method ends (line 20) the variable doesn't exist anymore. In other words, in line 5 of your second piece of code, field is undefined. (Also, what is Field? You haven't defined that anywhere).
You need to store the array somewhere so that the main method can access it.
Matthew Brown wrote:Hi Bruno. Welcome to The Ranch!
In general, we'd ask exactly what you mean by "it doesn't work", but I can notice a few problems.
The first think I notice is the line:
Field.printField(field);
That calls a static method printField on the class Field, and passes it the reference field. There are two problems there. You don't have a Field class, and you don't have a field reference.
Also, in your createField method - you create an array called spelplan, and then start setting values in field, which doesn't exist. Are you sure that's the code you're using, or have you done a cope-and-paste and not changed everything you need to?
Anyway, back in your main method: change Field to Array and you look like you have a viable way of printing out an array once you've got it. So the first thing you need to do in the main method is create the array - or call a method that will return a newly created array.