Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 17th, 2005, 12:34 AM   #1
thenewkid
Programmer
 
Join Date: Sep 2004
Location: New Jersey
Posts: 36
Rep Power: 0 thenewkid is on a distinguished road
Send a message via AIM to thenewkid
lost

hey...
im was writing the code for the game of life..u prob have herd about that ..im almost done...i just need add the code which determines which gameboard the user wants to use...here i got stuck lol..at the very end...
im gettin .class expected error
here is the sample code with the identical error......

public class test
{
	public static void main(String[] args)
	{
		int sel = 2;

		int array[][] = null;
		
		if(sel == 2)
		    int array[][] = {{2,2},
				         {2,2}};
		else
		    int array[][] = {{0,0},
				         {0,0}};

		for(int i = 0; i < array.length; i++)
		{
			 System.out.println();
			 for(int j = 0; j < array[0].length; j++)
			  {
				System.out.print(array[i][j] + " ");
			  }
		}
	}
}

all i found on the net was that this error is commonly caused by extra, or missing bracket...please help!!
thanx
thenewkid is offline   Reply With Quote
Old Feb 17th, 2005, 12:44 AM   #2
Cup O' Java
Newbie
 
Cup O' Java's Avatar
 
Join Date: Dec 2004
Posts: 18
Rep Power: 0 Cup O' Java is on a distinguished road
are you missing packages or dont you need them for this program?
Cup O' Java is offline   Reply With Quote
Old Feb 17th, 2005, 12:58 AM   #3
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 398
Rep Power: 5 xavier is on a distinguished road
Send a message via Yahoo to xavier
You could do it this way:
	 public class test
{
	public static void main(String[] args)
	{
		int sel = 2;

		int array[][] =new int[2][2];

		if(sel == 2)
		for(int i=0;i<2;i++)
		   for(int j=0;j<2;j++){
		    array[i][j] =2;
		    }
		else
		for(int i=0;i<2;i++)
		   for(int j=0;j<2;j++){
		    array[i][j] =0;
		    }

		for(int i = 0; i < array.length; i++)
		{
			 System.out.println();
			 for(int j = 0; j < array[0].length; j++)
			  {
				System.out.print(array[i][j] + " ");
			  }

		}
			 System.out.println();
	}
}
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Feb 17th, 2005, 9:39 AM   #4
thenewkid
Programmer
 
Join Date: Sep 2004
Location: New Jersey
Posts: 36
Rep Power: 0 thenewkid is on a distinguished road
Send a message via AIM to thenewkid
im sorry i didnt state my question right....im gonna tell u what i want to do...
if the user presses 1 for example i want to initilize the array with certain values...then if user presses 2 i want to initilize the same array with different values...i want to use the same array name in both cases, and i want the case to determine what values this array will be initialized with....i hope this will help u to figure this out...
thanks again!!
thenewkid is offline   Reply With Quote
Old Feb 17th, 2005, 9:48 AM   #5
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
u can put a file of set grid size and values for each data set, plus a blank grid for random population. read the values from the file into the array to populate the "grid".
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Feb 17th, 2005, 10:33 AM   #6
thenewkid
Programmer
 
Join Date: Sep 2004
Location: New Jersey
Posts: 36
Rep Power: 0 thenewkid is on a distinguished road
Send a message via AIM to thenewkid
good idea...but the problem is im not allowed to use file input
thenewkid is offline   Reply With Quote
Old Feb 17th, 2005, 10:52 AM   #7
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
these are the 2 ways of initilising an array in java:

arrayType arrayName[][] = {{row 1},{row2}};

or for a fixed lenght one

arrayType arrayName[][] = new arrayType[x][y];

you cant add values in teh {{},{}} way after you have created the initial arry by the looks of things (looked in book), so either use 2 arrays and just use an if statement on the sel so

if (sel ==2){
        print array1;
}else{
       print array2;
}

or use xaviars way.
Berto is offline   Reply With Quote
Old Feb 17th, 2005, 5:17 PM   #8
thenewkid
Programmer
 
Join Date: Sep 2004
Location: New Jersey
Posts: 36
Rep Power: 0 thenewkid is on a distinguished road
Send a message via AIM to thenewkid
oki ..i got it ...i hadda use different names for arrays in different cases...i just wonder why it didnt work the way i wanted...anyways, thanks for your help...i really appreciate it...
thenewkid is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:15 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC