-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".

8
  • 3
    Why not just use an array? String[] strings = {"Hello1", "Hello2"}; System.out.println(strings[1]); Commented Jul 5, 2020 at 6:07
  • I think what you want is a function with n as the parameter. Commented Jul 5, 2020 at 6: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. Commented Jul 5, 2020 at 6:10
  • @flakes Yeah, I think so too. Commented Jul 5, 2020 at 6:13
  • Your question is not really clear to me @Ronald Liu. Can you please ask your question in more detail? Commented Jul 5, 2020 at 6:14

5 Answers 5

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());
}
Sign up to request clarification or add additional context in comments.

1 Comment

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"])
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);
}
}

Comments

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

Comments

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.

Comments

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);
} 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.