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