File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how can i iterate my output?

 
Jinggoy Estrada
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys can you help me in my code?




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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jinggoy Estrada
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't vary names of variables, only their size and contents. But you can do something like
 
prateek chaudhari
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I Just want to ask I am not sure, can "Java Reflection" help him? :roll:
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".

(By the way, I don't think the smiley you used means what you think it means: http://faq.javaranch.com/java/SaloonGraemlins)
[ January 17, 2008: Message edited by: Ulf Dittmer ]
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is just my opinion...

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?
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic