0

Possible Duplicate:
Use a variable within a variable? Java

Hi this is a general programming related question more so than a java question. If i have a program like this :

public class tester {

    public static void main(String[] args) {
        int i = 2;
        String text = "Hello";
        String text2  = "world";

        System.out.println(text);
        System.out.println(text + i);
    }    
}

I want to print "hello", then "world" but instead i print "hello" and then "hello2". is there anyway i can access the variable text2 by appending the counter i to the variable test? I need a solution for this for a much more complicated program i am working on

Thanks, Matt

2
  • Do you need to loop over a list of variables and print them all out? Easy solution would be to use a List and iterate over that rather than use individual variables. Commented Aug 10, 2011 at 10:04
  • You can't do it without using arrays. Commented Aug 10, 2011 at 10:38

5 Answers 5

2

You can do this using an array:

String[] text = new String[2];
text[0] = "Hello";
text[1] = "world";

System.out.println(text[0]);
System.out.println(text[1]);

You can also index the array using a variable:

int i = 1;
System.out.println(text[i]);

Java does not generally support constructing (local) variable names at runtime. This is true of most (but not all) programming languages.

Sign up to request clarification or add additional context in comments.

Comments

2

You can't generally do this in Java - certainly not for local variables. Normally if you want to map a string or integer to a value, you should use a Map<String, ...>, a Map<Integer, ...> or an array instead of separate variables.

For instance or static variables you could use reflection to achieve this, but I would strongly advise you not to. Basically you should treat variables as separate storage locations which happen to have names at compile time, not a mapping from names to values. If you need the latter, create that mapping explicitly.

Comments

2

Maybe you want to look at arrays:

String[] texts = { "Hello", "world"};

for (int i=0; i<2; i++)
    System.out.println(texts[i]);

for (String text : texts)
   System.out.println(text);

Comments

0

It is because Java evaluates the expression inside System.out.println(expr) first, then convert it to a String. When Java evaluates the expression, it follows the left-to-right rule.
So, you get

System.out.print("hello"+2); // hello2   

Comments

0

You cannot access local variable by name, however you can access fields by name.

public class Tester {
    public static final String text = "Hello";
    public static final String text2  = "world";

    public static void main(String[] args) {
        int i = 2;
        System.out.println(Tester.class.getField("text").get(null));
        System.out.println(Tester.class.getField("text"+i).get(null));
    }
}

This practice is discouraged for obvious reasons.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.