Solved

Variable in new variable name

Posted on 2003-11-19
9
328 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 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