*
The moose likes Beginning Java and the fly likes how can i iterate my output? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how can i iterate my output?" Watch "how can i iterate my output?" New topic
Author

how can i iterate my output?

Jinggoy Estrada
Greenhorn

Joined: Dec 28, 2007
Posts: 3
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
Marshal

Joined: Mar 22, 2005
Posts: 41083
    
  43
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.



Ping & DNS - my free Android networking tools app
Jinggoy Estrada
Greenhorn

Joined: Dec 28, 2007
Posts: 3
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: 41083
    
  43
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
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: 41083
    
  43
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

Joined: Oct 02, 2003
Posts: 11153
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can i iterate my output?
 
Similar Threads
break Statements and flag
help with Locking Java Code to MAC adress
nested loops
How This Java Program Executes ?
Resize the multi dimension array