Solved

Variable in new variable name

Posted on 2003-11-19
9
300 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
Comment Utility
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
Comment Utility
sorry, declaration should be:

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

Author Comment

by:JTWolfman
Comment Utility
ok, i need to be able to use the pages[#] as a variable name now, not just print them
0
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
pages[#] IS a variable
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Expert Comment

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

??
0
 

Author Comment

by:JTWolfman
Comment Utility
i need to be able to declare something like

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

Expert Comment

by:JakobA
Comment Utility
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
Comment Utility
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
Comment Utility
thanks, i didnt know if it could be done without arrays, but with works so i split the points
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now