Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Evaluating a string 1

Status
Not open for further replies.

robherc

Programmer
Apr 20, 1999
921
US
OK, I'm still in the learning process with Java, but I have a decent bit of experience with JScript and QBasic.

I'm trying to create a for loop to access several buttons that I've created, variable names b1, b2, b3, b4....b16.

What class/command do I need to use to evaluate a string as a variable name so I can do this? (I.E. the red highlighted portion of the class below...I know that's wrong though, but it hopefully demonstrates what I'm wanting)
Code:
JButton b1, b2, b3, b4, b5;

public static void main(string[] args)
{
 for(byte buttoN = 1; buttoN <= 5, buttoN++)
 {
  [red]eval("b"+buttoN)[/red] = new JButton();
 }
}
 
Does Array or ArrayList mean anything in this matter [morning]
 
You're right, I could avoid this problem in this particular situation by placing the button objects in an array... But my primary concern here is learning the Language better. So I'm wanting to use the string if it's possible, or, if I find that it's impossible, I'll have to adjust my programming practices in a major way, as I've always had a high tendency to use string evaluators fairly heavily in my programs prior to now :-S
 
Must have been an interpreted language like php or some xBase dialect, I guess ;-)
Not with 'properly' compiled languages, you can't create new variables on the fly, that's where the Arrays/ArrayLists come in.

Better grab a good book on Java then, before you hit more walls on the way in.

HTH
 
lol, ok thanks...was just wanting to use it to access already created objects, but I understand the reason why it's not usable...just had to check ;)

FYI, I'm using a book too, just the kind of person who constantly infuriates trainers by getting 3 steps ahead of the instruction ;) Java all-in-one desk reference for dummies seems a fairly good starting point, but be warned, I've yet to read 3 consecutive pages without finding at least 1-2 proof-reading faux-pas :-/

I hope this helps;
robherc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top