Solved

Variable in new variable name

Posted on 2003-11-19
9
336 Views
Last Modified: 2010-03-31
im trying to use a for loop to create numbered variables without doing it by hand

for(int i=0;i<length;i++) {
Page#  where the # is the value of i
}

please post if this is possible and code to do it
0
Comment
Question by:JTWolfman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
ID: 9784419
that's what arrays are for:

int pages[length];

for (int i=0;i<pages.length;i++) {
   pages[i] = i;
}

System.out.println (pages[0]);
System.out.println (pages[1]);
System.out.println (pages[2]);
etc...
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 9784428
sorry, declaration should be:

int[] pages = new int[length];
0
 

Author Comment

by:JTWolfman
ID: 9784521
ok, i need to be able to use the pages[#] as a variable name now, not just print them
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 18

Expert Comment

by:bobbit31
ID: 9784525
pages[#] IS a variable
0
 
LVL 3

Expert Comment

by:InNoCenT_Ch1ld
ID: 9784544
>>ok, i need to be able to use the pages[#] as a variable name now, not just print them

??
0
 

Author Comment

by:JTWolfman
ID: 9784555
i need to be able to declare something like

String pages[#] = "Page";
0
 
LVL 15

Expert Comment

by:JakobA
ID: 9784711
Sorry, what you are describing are facilities you have in script languages, not in compiled languages like Java.
The upside of that is that Java programs run about 100 times faster than script programs.
But you cannot (at this time at least) have both.

would your need be filled by a static class with 2 methods in it.

  StC.setVar( name, value );   // name is a string (fx "Page"+2 ) and var may be a strin or an int or a float or ...
and
  Object n = Stc.getVar( name );  // give you back the variable value for that name in an object

That would be fairly easy to make using a hashmap.

regards JakobA
0
 
LVL 7

Assisted Solution

by:grim_toaster
grim_toaster earned 30 total points
ID: 9785274
How about declaring an Object to be held in your array,

private class Page {
  public String name;
  public Page(String name) {
    this.name = name;
  }
}

And declare your array as:

Page[] pages = new Page[length];

Then to set the page name use pages[i].name = "hello";

Or, create public get/set methods to access your name.  This approach also allows you to add other items to the page with very llittle effort on  your part!
0
 

Author Comment

by:JTWolfman
ID: 9790231
thanks, i didnt know if it could be done without arrays, but with works so i split the points
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question