954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

declaring alot of varibles in a for loop???

I am making a game and need a hell of alot of varibles. it is all so GUI so I need alot of Jbuttons. can I do it like something what I have below???

char[] mine=new char[3];
for(int count=0;count<10;count++){
//String add = "one"+count;
JButton mine[count] = new JButton("");
}

heres the error
C:\Documents and Settings\Adam\Desktop\work\MineSweep.java:42: mine is already defined in MineSweep()
final JButton mine[count] = new JButton("");

ultimate_fusion
Light Poster
44 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

You've defined "mine" twice.

char[] mine
JButton mine

which is it?

int count = 10
JButton[] guiButtons = JButton[count];
 
for(int i=0; i<count;i++)
{
	guiButtons[i] = new JButton();
}
Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

First of all, that wont work..Plus the array would be indexed out of bounds if it did. What are you trying to do with the chars? If I knew, I could give you some better code:

JButton[] myButtons = new JButton[10];
char mine = '*';

for (int i=0; i<myButtons.length; i++)
{
       myButtons[i] = new JButton(" ");
       myButtons[i].addActionListener(this);
}

public void actionPerformed(ActionEvent ae)
{ 
              //choose three buttons you want as bombs
              if (ae.getSource() == myButtons[0])
             {
                     myButtons[0].setText(mine + "");
             }
             else if (ae.getSource() == myButtons[1])
             {
                      myButtons[1].setText(mine + "");
             }
              else if (ae.getSource() == myButtons[2])
              {
                      myButtons[2].setText(mine + "");
              }
}


I guess that's something like what your wanting...If it's not, give me some more information, and I can help some more.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You