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 4 of 4

Thread: HELP: UNABLE TO CREATE AND PRINT A 3-DIMENSIONAL ARRAY

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation HELP: UNABLE TO CREATE AND PRINT A 3-DIMENSIONAL ARRAY

    //I created this program which creates 3-dimensional array and prints elements of the array

    public class ThreeDimensionalArray
    {
     
    	public static void main(String[] args)
    	{
     
    		int[][][] myArray;
     
    		myArray = {  {  {11,12,13,14},
    					    {15,16},
    					    {17},
    					    {18,19}
    				     },
     
    				     {  {21,22},
    				        {23,24,25},
    				        {26,27,28,29}
     
    				  }; // end of 3-D array
     
     
    			//a loop to print the values of a three-dimensional array
     
    			for(int i=0; i<myArray.length; i++)
    			   for(int j=0; j<myArray[i].length; j++)
    			     for(int k=0;k<myArray[j]).length;k++)
    			        System.out.print(myArray[i][j][k]);
     
     
     
    				} //ends main
     
    			} // ends class ThreeDimensionalArray


    But the program gives me this error when compiling it:

    E:\JAVA_STUNTS\Array_Work>javac ThreeDimensionalArray.java
    ThreeDimensionalArray.java:11: illegal start of expression
    myArray = { { {11,12,13,14},
    ^
    ThreeDimensionalArray.java:11: not a statement
    myArray = { { {11,12,13,14},
    ^
    ThreeDimensionalArray.java:11: ';' expected
    myArray = { { {11,12,13,14},
    ^
    ThreeDimensionalArray.java:11: illegal start of expression
    myArray = { { {11,12,13,14},
    ^
    ThreeDimensionalArray.java:12: not a statement
    {15,16},
    ^
    ThreeDimensionalArray.java:12: ';' expected
    {15,16},

    ^

    Please, can any one help me figure out the error??
    Last edited by helloworld922; July 3rd, 2011 at 11:54 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP: UNABLE TO CREATE AND PRINT A 3-DIMENSIONAL ARRAY

    Put the definition and the assignment in one statement.
    nt[][][] myArray = new int[][][] { { {11,12, ...
    Otherwise move the assignment part into a constructor or method.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP: UNABLE TO CREATE AND PRINT A 3-DIMENSIONAL ARRAY

    ...thanks alot,,,i solved one part of the problem,,,the only remaining exception that is thrown is an ArrayIndexOutOfBounds , in the last for loop!!!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP: UNABLE TO CREATE AND PRINT A 3-DIMENSIONAL ARRAY

    What code controls the number of iterations in the last loop?

Similar Threads

  1. Two-dimensional boolean array?
    By tcmei in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 17th, 2011, 08:32 PM
  2. Single Dimensional Array Help!
    By Allicat in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 15th, 2011, 12:01 PM
  3. Two-Dimensional Array and Loops
    By astrojunk in forum Java Theory & Questions
    Replies: 2
    Last Post: February 11th, 2011, 07:18 AM
  4. 2 dimensional array alternative ???
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: February 23rd, 2010, 06:18 PM
  5. Unable to create a new connection!
    By fh84 in forum JDBC & Databases
    Replies: 1
    Last Post: November 20th, 2009, 05:58 PM