Solved

Variable in new variable name

Posted on 2003-11-19
9
320 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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 …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

786 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