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

    Default 2D array addition error 'Not a Statement''

    Hi I am new to java programming. I want to write a java program for 2D array addition. My code is:
    Java Code:
    class Matrixadd
    {
    
    
    public static void main(String...s)
    	{
    	int x[][]={
    	   	{1,2,3},
    		{4,5,6},
    		{7,8,9},
    	  };
    
    int y[][]={
    	   	{5,8,11},
    		{6,9,12},
    		{7,10,13},
    	  };
    	Matrixadd M=new Matrixadd();
    	int [][]z=M.matrixadd(x,y);
    	for(int i=0;i<z.length;i++)
    		{
    		System.out.println(z[i]);
    		}
    	}
    
    
    	 int [][] matrixadd(int x[][],int y[][])
    	{
    	int [][]c=new int[x.length][y.length];
    
    	for(int i=0;i<x.length;i++)
    		{
    			for(int j=0;j<y.length;j++)
    				{
    					
    				c[i][j]=x[i][j]+y[i][j];	
    					
    	
    				}
    		}
    	return c;
    	}
    }
    I m getting a 'Not a statement' error at line 36.Please help..

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: 2D array addition error 'Not a Statement''

    That stuff compiles fine for me; what you're showing us is not what you're trying to compile ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    iamsethi is offline Member
    Join Date
    Dec 2014
    Posts
    19
    Rep Power
    0

    Default Re: 2D array addition error 'Not a Statement''

    Thanks! a lot.It was compiling successfully previously but giving the Array index out of bound exception error on execution. I've updated now it's running as expected
    Java Code:
    class Matrixadd
    {
    
    
    public static void main(String...s)
    	{
    	int x[][]={
    	   	{1,2,3},
    		{4,5,6},
    		{7,8,9},
    	  };
    
    int y[][]={
    	   	{5,8,11},
    		{6,9,12},
    		{7,10,13},
    	  };
    	Matrixadd M=new Matrixadd();
    	int [][]z=M.matrixadd(x,y);
    	System.out.println("First matrix");
    	for(int i=0;i<3;i++){
    	for(int j=0;j<3;j++)
    		{
    		System.out.print(x[i][j] + " ");
    		}System.out.println();	     }
    	System.out.println("Second matrix");
    	for(int i=0;i<3;i++){
    	for(int j=0;j<3;j++)
    		{
    		System.out.print(y[i][j] + " ");
    		}System.out.println();	     }
    	System.out.println("Sum of the matrix");
    	for(int i=0;i<3;i++){
    	for(int j=0;j<3;j++)
    		{
    		System.out.print(z[i][j] + " ");
    		}System.out.println();	     }
    	}
    
    
    	 int [][] matrixadd(int x[][],int y[][])
    	{
    	int [][]c=new int[3][3];
    
    	for(int i=0;i<x.length;i++)
    		{
    			for(int j=0;j<y.length;j++)
    				{
    					
    				c[i][j]=x[i][j]+y[i][j];	
    					
    	
    				}
    		}
    	return c;
    	}
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: 2D array addition error 'Not a Statement''

    Quote Originally Posted by iamsethi View Post
    Thanks! a lot.It was compiling successfully previously but giving the Array index out of bound exception error on execution. I've updated now it's running as expected
    But according to your OP, the code wasn't compiling (so it certainly wouldn't run), but now you claim that the code did compile but showed a runtime error; it would've been nice if you'd made up your mind before posting and leaving the reader(s) of your thread in riddles ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    iamsethi is offline Member
    Join Date
    Dec 2014
    Posts
    19
    Rep Power
    0

    Default Re: 2D array addition error 'Not a Statement''

    i am unable to post new thread...please help

  6. #6
    iamsethi is offline Member
    Join Date
    Dec 2014
    Posts
    19
    Rep Power
    0

    Default Re: 2D array addition error 'Not a Statement''

    import java.util.Scanner;
    class Prime
    {

    public static void main(String args[])
    {
    Prime p=new Prime();
    Scanner in = new Scanner(System.in);
    int t=in.nextInt();
    if(t<=10)
    {
    int [][]d=new int[t][2];
    for(int i=0;i<d.length;i++)
    {
    for(int j=0;j<2;j++)
    {
    d[i][j]=in.nextInt();
    }
    }
    for(int i=0;i<t;i++){
    p.prime(d[i][0],d[i][1]);
    System.out.println( " ");}



    }
    else return;

    }
    void prime(int m,int n)

    {
    int []a=new int[100];
    for(int i=0;i<a.length;i++)
    {
    a[i]='0';
    }
    int c=0;int count=0;
    for(int i=m;i<=n;i++)
    {
    a[c]=i;
    c++;
    }
    for(int i=0;i<a.length;i++)
    {
    for(int j=2;j<a[i];j++)
    {
    if((a[i]%j)==0)
    a[i]='0';
    }
    }

    for(int i=0;i<a.length;i++)
    {
    if((a[i]!=1)&&a[i]!='0')
    System.out.println(a[i]);
    }
    }







    }

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: 2D array addition error 'Not a Statement''

    There is no question in the vicinity of your last post; are you assuming that we're psychic?

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 5
    Last Post: 01-16-2014, 10:22 AM
  2. Replies: 2
    Last Post: 03-26-2012, 04:27 PM
  3. if statement using an array. please help! :(
    By jumoo in forum New To Java
    Replies: 11
    Last Post: 08-24-2011, 06:54 AM
  4. using an if statement to populate an array
    By MrJinx in forum New To Java
    Replies: 3
    Last Post: 04-30-2011, 12:01 PM
  5. "not a statement" error to array declaration
    By SpaceMonkey in forum New To Java
    Replies: 3
    Last Post: 11-25-2010, 11:01 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
  •