Results 1 to 4 of 4
  1. #1
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default Filling 2D Array

    Hi all, I'm having a little trouble getting my head around
    Java Code:
    static void fill(char[] a, char val)]
    I'm not entirely sure what its telling me to do.
    This is the section of code containing this:
    Java Code:
    public static class Grid
    	{
    		Grid[][] twoDimGrid = new Grid[31][31]; // 31x31 grid
    		private static void fill(char[] twoDimGrid, char ' ');
    		void print(){System.out.println(this.twoDimGrid);}
    	}
    From what i've gather i type "char[]" then the name of array im filling "twoDimGrid" then i use a comma and enter char val. So i type "char ' '" However i get errors on eclipse saying:

    Syntax error on token "' '", invalid VariableDeclaratorld

    I have no idea what this means, if i cant use a space as a char what else should i use for my grid?

  2. #2
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    i managed to get it t compile with no errors, however it does not display the array, any ideas why?
    Java Code:
    //define SnakeGame class
    public class SnakeGame
    {
    	char s = ' ';
    	//create main method(All Java prgrams need one to run ;))
    	public static void main(String[] args)
    	{
    		
    	}
    	//Create a 2D grid for the game to be played on
    	public static class Grid
    	{
    		Grid[][] grid = new Grid[31][31]; // 31x31 grid
    		static void fill(char grid[][], char _){}
    		void print(){System.out.println(this.grid);}
    	}
    	
    }

  3. #3
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default something like this

    Quote Originally Posted by Nakira View Post
    i managed to get it t compile with no errors, however it does not display the array, any ideas why?
    It goes something like this.I'm not proficient on the enhanced for loop, probably have one or more blunders but it is something like this.
    Java Code:
    // preliminary
    public class SnakeGame
    {
        //create main method(All Java prgrams need one to run ;))
        public static void main(String[] args)
        {
            Grid grid = new Grid();
            grid.fill();//
            grid.print();//
        }
        //Create a 2D grid for the game to be played on
        public class Grid
        {
            char s = ' ';
            char [][] grid = new char[31][31]; // 31x31 grid
            void fill()
            {
                for( char[] row : grid )
                {
                    for( char row : column )
                    {
                        column = s;
                    }
                }
            }
            void print()
            {
                for( char[] row : grid )
                {
                    for( char row : column )
                    {
                        System.out.println(column);
                    }
                }
            }
        }
    }
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  4. #4
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    A massive thanks for this, i can see where your coming from now and I'm beginning to understand it better, however that code produced 7 errors. :s
    Java Code:
    SnakeGame2.java:7: non-static variable this cannot be referenced from a static context
            Grid grid = new Grid();
                        ^
    SnakeGame2.java:20: row is already defined in fill()
                    for( char row : column )
                              ^
    SnakeGame2.java:20: cannot find symbol
    symbol  : variable column
    location: class SnakeGame2.Grid
                    for( char row : column )
                                    ^
    SnakeGame2.java:22: cannot find symbol
    symbol  : variable column
    location: class SnakeGame2.Grid
                        column = s;
                        ^
    SnakeGame2.java:30: row is already defined in print()
                    for( char row : column )
                              ^
    SnakeGame2.java:30: cannot find symbol
    symbol  : variable column
    location: class SnakeGame2.Grid
                    for( char row : column )
                                    ^
    SnakeGame2.java:32: cannot find symbol
    symbol  : variable column
    location: class SnakeGame2.Grid
                        System.out.println(column);
                                           ^
    i would try and sort them but i dont understand. :s

Similar Threads

  1. Array help
    By EternalSolitude in forum New To Java
    Replies: 9
    Last Post: 10-28-2008, 08:10 AM
  2. 3 by 3 array
    By fromme87 in forum New To Java
    Replies: 5
    Last Post: 10-02-2008, 09:34 AM
  3. Replies: 4
    Last Post: 09-13-2008, 12:07 PM
  4. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:08 PM
  5. Would appreciate your help with 2d Array..
    By cloudkicker in forum New To Java
    Replies: 1
    Last Post: 02-11-2008, 03:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •