what i wanna do is to print the variables mac1, mac2, mac3 inside the for loop instead of
[ January 17, 2008: Message edited by: paul apoderado ]
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35220
7
posted
0
That's not possible. If you want to iterate through variables like this, create an array. Note that array indexes start at 0, and that rectangular brackets are used for the index.
what if I wanna initialize / create different arrays depending on my user input??
for example user input is 3
can i create?
array1[]; array2[]; array3[];
inside a for loop??
thanks!
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35220
7
posted
0
You can't vary names of variables, only their size and contents. But you can do something like
prateek chaudhari
Greenhorn
Joined: Oct 14, 2006
Posts: 11
posted
0
I Just want to ask I am not sure, can "Java Reflection" help him? :roll:
Prateek<br />SCJP 1.5
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35220
7
posted
0
Yes, reflection could be used, but it would be a very obscure and complicated solution. This being the beginner forum, we should not get into that, but rather talk about how to use Java objects to solve the problem.
I am of course assuming that the actual problem is not "how to access variables called mac1, mac2, ..." but rather "how to store a varying a number of variables under the name mac".
I don't understand why you want to change the name of the variable in the code based on user input.
The variable name is used by the PROGRAMMER to keep track of stuff - the user should have no idea what's happening inside. Why should the user care if I name my variable Age, howOld, yearsSinceBorn, or q7dkjf3443?
Never ascribe to malice that which can be adequately explained by stupidity.