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.

Results 1 to 7 of 7

Thread: Declaring static character array

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Declaring static character array

    I can't figure out how to declare a static two dimensional array.
    I tried...

    static char [][] array = new char [numberOfRows][numberOfColumns];

    static final char [][] array = new char [numberOfRows][numberOfColumns];

    can't figure it out. Please help. Thanks!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Declaring static character array

    ..and what happened when you tried either of those in your code? Post an SSCCE with the full errors you receive.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    My Mood
    Lurking
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Declaring static character array

    You may need to initialize the numberOfRows and numberofColumns variables first and give them values or else you will get an ArrayIndexOutOfBounds error when you try to access them...


    static int numberOfRows = 10;
    static int numberOfColumns = 20;
    static char [][] array1 = new char [numberOfRows][numberOfColumns];
    static final char [][] array2 = new char [numberOfRows][numberOfColumns];

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Declaring static character array

    I get illegal start of expression and other wierd errors saying the array cannot be found, but it says them on lines that don't even have code on them???

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Declaring static character array

    Post an SSCCE (see my signature if you don't know what this is)...we can guess at what's wrong, but guesses are just that.

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    red means error
    blue means critical parts

    //initialize temporary field
    String temp = "";
    //open the file
    File file = new File("maze.txt");

    Scanner input = new Scanner(file);

    //read data from file and store it
    temp = input.next();
    int numberOfRows = Integer.parseInt(temp);

    temp = input.next();
    int numberOfColumns = Integer.parseInt(temp);


    String totalMaze = "";

    //Read maze and store it in a single line String
    while (input.hasNext())
    {

    totalMaze = totalMaze + input.next();

    }

    //initiate the maze array
    static char [][] array = new [numberOfRows][numberOfColumns];


    }// end main method
    ----------------------------------------------------------------------------------------------------------------------------------
    //static method findTreasure to find the $
    public static String findTreasure (int rowIndex, int colIndex)
    {
    Initiate answer to hold the String of the location of the treasure
    String answer = " ";
    if (array[rowIndex][colIndex] == '$')
    {
    answer = "The treasure found at location [" + rowIndex + "][" + colIndex + "]";
    return answer;
    }
    else if (array[rowIndex][colIndex] == '#')
    {
    findTreasure(rowIndex+1, colIndex);
    findTreasure(rowIndex, colIndex+1);
    findTreasure(rowIndex-1, colIndex);
    findTreasure(rowIndex, colIndex-1);
    }
    else if (array[rowIndex][colIndex] == 'o')
    {
    findTreasure(rowIndex+1, colIndex);
    findTreasure(rowIndex, colIndex+1);
    findTreasure(rowIndex-1, colIndex);
    findTreasure(rowIndex, colIndex-1);
    array[rowIndex][colIndex] = 'X';
    }
    return answer;

    }// end method findTreasure



    obviously the red error is a "cannot find symbol" error.
    This is suppose to be a recursive method I still need some work on the algorithm, but I wasn't sure how to go about making the char array
    readable from the static method??

    Therefore, I tried to make the array static, but then I get other multiple errors such as "cannot find symbol: variable array" on this line:
    for (int row = 0; row < numberOfRows; row++)

    I also got the "illegal start of expression" on this line: static char [][] array = new char [numberOfRows][numberOfColumns];

    So, basically I cant decide which way to go about making the array readable from the static method??
    sorry if this post is too long

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Declaring static character array

    You are trying to declare/initialize the array inside a method. Perhaps you should review how instance and class variables should be declared:
    Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

Similar Threads

  1. Error writing to an array with the static modifier.
    By handuel in forum Collections and Generics
    Replies: 2
    Last Post: December 25th, 2011, 11:10 AM
  2. Comparing a character to an array of strings
    By Hashmeer169 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 4th, 2011, 07:55 PM
  3. Can I pass character array to new method?
    By harrydb11 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 21st, 2011, 01:56 PM
  4. array element declaring problem
    By jack_nutt in forum Collections and Generics
    Replies: 11
    Last Post: August 1st, 2011, 06:29 PM
  5. Character Array
    By p_dibb in forum Collections and Generics
    Replies: 1
    Last Post: February 16th, 2011, 10:08 AM