Results 1 to 13 of 13
  1. #1
    anmol_b is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Question ';' expected error

    ';' expected error-untitled.pngi get an error as "error: ';' expected int checkNeighbour(int m,n) " and "error: ';' expected int checkNeighbour(int m,n) "
    ^ ^

    when i compile the following code:

    Java Code:
    import java.io.*;
    
    class Zycus {
    	public static void main(String arg[ ])
    	{
    
    	DataInputStream in = new DataInputStream(System.in);
    
    	int[ ][ ] inputGrid = new int[6][6];  
    
    	for(int i=0; i<6 ; i++)
    		{	
    		for(int j=0; j<6 ; j++)
    			{ System.out.println("Enter Alive :1 or Dead : 0 for cell no.- " + i + "," + j);
    			inputGrid[i][j]=Integer.parseInt(in.readLine());
    			}
    		}
    
    	System.out.println("The Grid entered is");
    	for(int i=0; i<6 ; i++)
    		{	
    		for(int j=0; j<6 ; j++)
    			{ System.out.print(inputGrid[i][j] + "	");}
    		System.out.println();
    		}
    
    	int[ ][ ] outputGrid = new int[6][6];
    
    	for(int i=0; i<6 ; i++)
    		{	
    		for(int j=0; j<6 ; j++)
    			{
    			if(inputGrid[i][j]==1)
    				{int c=checkNeighbour(i,j);
    					if(2<=c && c<8)
    						{
    						outputGrid[i][j]=1;
    						}
    					 else if (c<2) 
    						{ outputGrid[i][j]=0; }
    				}
    			else if(inputGrid[i][j]==0)
    				{int c=checkNeighbour(i,j);
    					if(c==3)
    						{ outputGrid[i][j]=1; }
    					else{ outputGrid[i][j]=0;}
    				}
    			}
    		}
    
    	System.out.println("First Generation of the Input Grid is:");
    	for(int i=0; i<6 ; i++)
    		{	
    		for(int j=0; j<6 ; j++)
    			{ System.out.print(outputGrid[i][j] + "	");}	
    		System.out.println();
    		}
    
    	int checkNeighbour(int m,n)
    		{
    		int[ ] temp=new int[8]; 
    		int count=0;
    
    		temp[0] = inputGrid[m-1][n-1];
    		temp[1] = inputGrid[m-1][n];      
    		temp[2] = inputGrid[m-1][n+1];
    		temp[3] = inputGrid[m][n-1];
    		temp[4] = inputGrid[m][n+1];
    		temp[5] = inputGrid[m+1][n-1];
    		temp[6] = inputGrid[m+1][n];
    		temp[7] = inputGrid[m+1][n+1];
    
    		for(int i=0; i<8; i++)
    			{ if ( temp[i] == 1)
    				{ count++;}
    			}
    
    		return count;
    		}
    
    	}
    }
    please help me rectify it!
    Attached Thumbnails Attached Thumbnails ';' expected error-untitled.png  
    Last edited by anmol_b; 07-09-2013 at 08:47 AM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: ';' expected error

    Jeez Java is even telling you the spot where it is going wrong; in stead of going "halp! an error, let me run to a forum!" why don't you try reading it and trying to understand what is wrong?

    Java can't compile your code, which means somewhere around the line it is complaining about, there is a mistake in your code's syntax. The problem I have with the code above is that the formatting is incredibly dire and unreadable; try formatting it more nicely so all the accolades match up and then you'll probably see yourself what the boo boo is.

  3. #3
    anmol_b is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: ';' expected error

    I get that! Obviously i went through the code 10 times. Couldnt find the syntax error! It is organized .. how much more should i format it?

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: ';' expected error

    Like everyone else does it!

    Java Code:
    // your code
    for(int j=0; j<6 ; j++)
                { System.out.print(outputGrid[i][j] + " ");}
    Java Code:
    // clean code
    for(int j=0; j<6 ; j++) {
        System.out.print(outputGrid[i][j] + " ");
    }

  5. #5
    anmol_b is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: ';' expected error

    Okay i did it. Yet the problem stays

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: ';' expected error

    And now that you cleaned it up, try looking for the problem again! There are MINUTES between your posts, that proves that you did exactly zero effort to look for the problem.

    Fine. It is quite clearly visible that the checkNeighbour() method is INSIDE the main() method.

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

    Default Re: ';' expected error

    Also, both parameters of the method need to be typed; "int n,m" won't do it ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    anmol_b is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: ';' expected error

    Yes. It is inside main() method because i wish to use inputGrid[][] array as a global variable for the method checkneighbour()

  9. #9
    anmol_b is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: ';' expected error

    @JosAh I didnt get you.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: ';' expected error

    Quote Originally Posted by anmol_b View Post
    Yes. It is inside main() method because i wish to use inputGrid[][] array as a global variable for the method checkneighbour()
    You have to learn the language rules, you can't just make stuff up yourself. Get a good book and follow it from start to finish.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: ';' expected error

    Quote Originally Posted by anmol_b View Post
    Yes. It is inside main() method because ...
    Stop right there. In Java, methods can't be nested.

    Also, Java doesn't have 'global variables'

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: ';' expected error

    Quote Originally Posted by anmol_b View Post
    @JosAh I didnt get you.
    You typed "int m,n" for a parameter list; that is not valid Java; both parameters need to have a type, i.e. "int m, int n"

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    anmol_b is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: ';' expected error

    I apologize! I created a separate class for checkNeighbour function and passed the desired array by calling it in the main() method via a constructor of that class.

    Java Code:
    import java.io.*;
    
    class checkNeighbour{
    int checkNeighbour(int m, int n, int[ ][ ] inputGrid)
    		{
    		int[ ] temp=new int[8]; 
    		int count=0;
    
    		temp[0] = inputGrid[m-1][n-1];
    		temp[1] = inputGrid[m-1][n];      
    		temp[2] = inputGrid[m-1][n+1];
    		temp[3] = inputGrid[m][n-1];
    		temp[4] = inputGrid[m][n+1];
    		temp[5] = inputGrid[m+1][n-1];
    		temp[6] = inputGrid[m+1][n];
    		temp[7] = inputGrid[m+1][n+1];
    
    		for(int i=0; i<8; i++)
    			{ if ( temp[i] == 1)
    				{ count++;
    				}
    			}
    
    		return count;
    		}
    	}
    class Zycus {
    
    
    	public static void main(String arg[ ]) throws IOException
    	{
    
    	DataInputStream in = new DataInputStream(System.in);
    
    	checkNeighbour ch = new checkNeighbour();
    	
    	int[ ][ ] inputGrid = new int[6][6];  
    
    	for(int i=0; i<6 ; i++)
    		{	
    		for(int j=0; j<6 ; j++)
    			{ System.out.println("Enter Alive :1 or Dead : 0 for cell no.- " + i + "," + j);
    			inputGrid[i][j]=Integer.parseInt(in.readLine());
    			}
    		}
    
    	System.out.println("The Grid entered is");
    	for(int i=0; i<6 ; i++)
    		{	
    		for(int j=0; j<6 ; j++)
    			{ System.out.print(inputGrid[i][j] + "	");
    			}
    		System.out.println();
    		}
    
    	int[ ][ ] outputGrid = new int[6][6];
    
    	for(int i=0; i<6 ; i++)
    		{	
    		for(int j=0; j<6 ; j++)
    			{
    			if(inputGrid[i][j]==1)
    				{int c=ch.checkNeighbour(i,j,inputGrid);
    					if(2<=c && c<8)
    						{
    						outputGrid[i][j]=1;
    						}
    					 else if (c<2) 
    						{ outputGrid[i][j]=0; 
    						}
    				}
    			else if(inputGrid[i][j]==0)
    				{int c=ch.checkNeighbour(i,j,inputGrid);
    					if(c==3)
    						{ outputGrid[i][j]=1; 
    						}
    					else{ outputGrid[i][j]=0;
    						}
    				}
    			}
    		}
    
    	System.out.println("First Generation of the Input Grid is:");
    	for(int i=0; i<6 ; i++)
    		{	
    		for(int j=0; j<6 ; j++)
    			{ System.out.print(outputGrid[i][j] + "	");
    			}	
    		System.out.println();
    		}
    
    	
    
    	}
    }
    Thank you everyone, for the input.

Similar Threads

  1. ';' expected error
    By name in forum New To Java
    Replies: 19
    Last Post: 12-23-2011, 08:09 AM
  2. ';' expected error
    By collwill in forum New To Java
    Replies: 3
    Last Post: 05-03-2011, 07:51 AM
  3. Error: ')' expected
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:32 AM
  4. Error: '{' expected
    By romina in forum New To Java
    Replies: 1
    Last Post: 07-26-2007, 09:34 AM
  5. MSG ERROR: : expected
    By Marty in forum New To Java
    Replies: 1
    Last Post: 05-31-2007, 02:21 AM

Tags for this Thread

Posting Permissions

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