Results 1 to 2 of 2
  1. #1
    iamsethi is offline Member
    Join Date
    Dec 2014
    Posts
    19
    Rep Power
    0

    Default Error ']' excpected in 2D Array Java Program

    I am trying this code .But everytime it is giving me this error on compiling
    Printdiagonal.java:21: error: ']' expected
    System.out.println(a[j,temp]);
    ^
    Printdiagonal.java:21: error: ')' expected
    System.out.println(a[j,temp]);
    ^
    Printdiagonal.java:21: error: illegal start of expression
    System.out.println(a[j,temp]);

    Java Code:
    class Printdiagonal
    {
    
    		public static void main(String...s)
    			{
    			Printdiagonal P=new Printdiagonal();
    		P.print(new int[][] {{1,567,4,3,33},{0,333,677,34243,8987,3434,324},{446,876,23,546565,332}});
    			}
    
    void print(int [][]a)
    
    	{
    	for(int i=0;i<a.length;i++)
    		{int temp=0;
    		
    		temp=i;
    		while(temp>=0)
    			{
    			for(int j=0;j<=i;j++)
    				{
    				System.out.println(a[j,temp]);
    				temp--;
    				}
    			}
    		}
    	}
    	
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Error ']' excpected in 2D Array Java Program

    The syntax for accessing an element in a 2 dim array is:
    Java Code:
      theArrayName[firstindex][secondIndex]
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Error With Array Program
    By ShadowWalker in forum New To Java
    Replies: 9
    Last Post: 03-15-2013, 03:14 PM
  2. Java Array Error
    By Captain Forge in forum New To Java
    Replies: 3
    Last Post: 11-22-2012, 12:44 AM
  3. Need help with beginner java array program??
    By sd1234 in forum New To Java
    Replies: 3
    Last Post: 01-31-2012, 08:24 AM
  4. Java program error
    By Taszk in forum New To Java
    Replies: 16
    Last Post: 06-06-2011, 05:59 AM
  5. error in java program..
    By mudit222 in forum New To Java
    Replies: 2
    Last Post: 12-17-2010, 09:32 AM

Posting Permissions

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