-1

I'm not sure how to explain but this example should show what I'm trying to do. I could just print (str1) but this is just an example.

public static void main(String[] args) {
        String str1 = "Hello1";
        String str2 = "Hello2";
        int n = 1;
        System.out.println(str+n);
    }

In this case, it should print "Hello1" and when n = 2, it should print "Hello2".

Ronald Liu
  • 47
  • 1
  • 2
  • 3
    Why not just use an array? `String[] strings = {"Hello1", "Hello2"}; System.out.println(strings[1]);` – flakes Jul 05 '20 at 06:07
  • I think what you want is a function with `n` as the parameter. – Faris Jul 05 '20 at 06:07
  • @Faris It seems like OP wants to dynamically access a variable by resolving the local variable name. That said, there are much easier alternatives to achieve a similar output. – flakes Jul 05 '20 at 06:10
  • @flakes Yeah, I think so too. – Faris Jul 05 '20 at 06:13
  • Your question is not really clear to me @Ronald Liu. Can you please ask your question in more detail? – fose Jul 05 '20 at 06:14
  • What is the question? – Ramesh Papaganti Jul 05 '20 at 06:31
  • You want to generate the name of a local variable at run time and refer to it as a local variable. Difficult to do but not impossible. Nonetheless not worth the effort, in my opinion. Refer to [Is there a java equivalent of the python eval function?](https://stackoverflow.com/questions/7143343/is-there-a-java-equivalent-of-the-python-eval-function) and also to [Is there an eval() function in Java?](https://stackoverflow.com/questions/2605032/is-there-an-eval-function-in-java) – Abra Jul 05 '20 at 07:03
  • Your code prints 'str+n', but you don't declare str. – NomadMaker Jul 05 '20 at 08:31

5 Answers5

2

It is not possible to modify variable name in runtime. Honestly, I don't know any programming language that allows to do this. You need to find other solution.

For example:

You can do use array like this:

public static void main(String []args){
    String[] lines = {"Hello1", "Hello2"};
    int n = 1;
    System.out.println(lines[n-1]);
}

(The main problem of this solution is that you can output only Hello1 or Hello2)

Also you can use string formatting:

public static void main(String []args){
    int n = 1;
    System.out.println(String.format("Hello%d", n));
}

Or you can just concatenate number and you text:

public static void main(String []args){
    int n = 1;
    System.out.println("Hello" + n);
}

Or you can use StringBuilder:

public static void main(String []args){
    int n = 1;
    System.out.println(new StringBuilder().append("Hello").append(n).toString());
}
  • With something like Python or Perl you can definitely do this. I'm sure there are many more where it is possible. Python example: `a1 = "test"; print(locals()["a1"])` – flakes Jul 05 '20 at 09:08
1

Hey bro here i change your code i am using the loop to concatenate the integer value with the string.Now you can also change the integer values by changing the starting and ending values of the loop i.e (i=1 this is starting value) and (i<=2 this is ending value). here is code:-

public static void main(String[] args) {
String str2=null;
String str1 = "Hello";
for(int i=1;i<=2;i++)
{
    str2=str1+i;
    System.out.println(str2);
}
}
Manish Tambe
  • 11
  • 1
  • 3
0

Not sure what you're asking specifically. str is undefined.

Is this what you're looking for:

public static void main(String[] args) {
        String str = "Hello";
        int n = 1;
        System.out.println(str+n);
}

It will print Hello1 when n = 1, and Hello2 when n=2

0

I think this is the solution that you are searching for:

public class MyClass {

public String str1 = "Hello1";
public String str2 = "Hello2";

public static void main(String args[]) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
    int n = 1;
    String fieldName = "str" + n;
    System.out.println(new MyClass().getClass().getField(fieldName).get(new MyClass()));
}
}

1-This code is getting the class named "MyClass".

new MyClass().getClass()

2-Accessing the field with the same name as the string that I created called "fieldName".

int n = 1;
String fieldName = "str" + n;
new MyClass().getClass().getField(fieldName)

3-Finally, get the value of the variable that is going to be displayed:

int n = 1;
String fieldName = "str" + n;
new MyClass().getClass().getField(fieldName)

Don't forget to add the throw exception code to your main method.

Cardstdani
  • 4,999
  • 3
  • 12
  • 31
0

I don't know if I get exactly what you want, but you can try something like this to get out a int from a String and use it.

public class Something{ 
static String[] str = {"hello1", "hello2", "hello3"};
static String regrex = "[^0-9]";     

public static void printCorrectHello(String[] array, int num) {
    for (int i=0 ; i<array.length ; i++) { 
    if (Integer.valueOf(array[i].replaceAll(regrex, "")) == num) 
    System.out.println(array[i]); 
    }   
}
public static void main(String[] args) {
printCorrectHello(str ,3);
} 
}
fluxxx
  • 1