my dog learned polymorphism
The moose likes Beginning Java and the fly likes Create new variables from a single variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Create new variables from a single variable" Watch "Create new variables from a single variable" New topic

Create new variables from a single variable

S. Hoysala

Joined: Dec 29, 2011
Posts: 3
Hi guys,

I wish to create new variables from a single variable. Let me explain. I am creating a hangman game. I have a Button in my program called alphbutt. And I have a String array in my program called alpharray. Alpharray, as you may have guessed, holds letters from A-Z. Alphbutt is simply a blank button. Now, I wish to create an Alphbutt1, Alphbutt2, Alphbutt3, etc. without declaring these new Alphbutt(s). The reason is so that each button is a unique variable. Let me clarify myself further. This is a small portion of my code.

As you can see, for every button I have initialized, I have added an ActionListener so that it goes to the actionPerformed method. However, if you recall, alphbutt is only equal to the final variable I have initialized, and that is Z. Therefore, whenever I enter the actionPerformed method, the method only executes for the value of Z, and for none other button. If I click on button M, for example, I enter the actionPerformed method, but since alphbutt is equal to Z, it only performs for Z, even though I want it to perform for M or any other letter I have in mind. This is why I want to make different variables from a single variable using a for loop. I want letter A to be alphbutt0, B to be alphbutt1, just any distinction between each button value.

If you are still confused, please ask me. Thank you for any and all replies.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

Mostly, I am confused as to why this was posted in a forum named "Meaningless Drivel". Generally, questions about how to write code go in our forums devoted to, well, writing code.

So, I'll move it for you.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
S. Hoysala

Joined: Dec 29, 2011
Posts: 3
Thank you, Fred, for doing that for me.
marc weber

Joined: Aug 31, 2004
Posts: 11343

One approach would be to create an array of Buttons...

Then instead of Alphbutt1, Alphbutt2, etc., you would have alphaButton[0], alphaButton[1], etc.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Jason Bullers
Ranch Hand

Joined: Dec 27, 2011
Posts: 91

It sounds to me like an issue with your ActionListener. Can you post the implementation of your actionPerformed() method?

One thing you can have a look at, just off the top of my head, is assigning each new button you create an "action command." That could be it's letter, for example. In your actionPerformed(), you could retrieve this String and do something with it.
I agree. Here's the link:
subject: Create new variables from a single variable
jQuery in Action, 3rd edition