• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

how can i iterate my output?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't vary names of variables, only their size and contents. But you can do something like
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I Just want to ask I am not sure, can "Java Reflection" help him? :roll:
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic