Reputation: 3
public class NewTest {
public static void main(String[] args)
{
String [][] table;
table = new String [4][4];
for(int y=0; y<4; y++){
for(int x=0; x<4; x++){
table[y][x] = " ~ " ;
}
}
int y, x;
for(y=0; y<4; y++)
{
System.out.print(y+": ");
for(x=0; x<4; x++)
System.out.print(table[y][x]+" ");
System.out.println();
}
}
public void table ()
{
System.out.println(table[2][2]);
}
}
//this is the line where I have problems !
System.out.println(table[2][2]);
Upvotes: 0
Views: 209
Reputation: 726809
The problem is that String [][] table
is local to the method where it is declared, and is, therefore, invisible to other methods of the class.
There are two ways of making it visible:
String [][] table
a static
member in the enclosing class (because main
is static
), orString [][] table
to the function as a parameter.The second solution is usually better:
// Here is the declaration of a method taking 2D table
public static void showTableCell(String [][] table) ...
...
// Here is a call of that method from main():
showTableCell(table);
Upvotes: 4