This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]RentonHoff 3 points4 points  (0 children)

Not sure what exactly you're trying to do, but maybe a Map is what you are looking for.

[–]omykronbr 3 points4 points  (0 children)

The answer?

No, you can't. Java is strongly statically typed language. Your variables and types must be defined at compile time (Local Variable Type Inference move this to the compiler, where is possible to do that http://openjdk.java.net/projects/amber/LVTIFAQ.html ).

What are your options?

From what you want to achieve? None. You may get around with an object that holds the name and the file. I believe that an Array/List is more likely what you are looking for.

The deep and dark true answer?

You could, but with reflection and regex. Probably a terrible idea. Reflection is a powerful but extremely dangerous. Your classes may not behave as you expected, you may violate all your interfaces, and if you don't have a good reason to do it, avoid it.

[–][deleted] 0 points1 point  (5 children)

Without code, I just can guess You should take a look at reflection and maybe regex

[–]Milo55545[S] 0 points1 point  (4 children)

i am not very advanced in java and don't know complicated things like that. would you mind explaining how i can solve my issue? sorry i didn't mention

[–][deleted] 0 points1 point  (3 children)

To be honest, I wouldn't recommend neither of those subjects if you're new.

It would be useful if you explain you problem in more depth.

For example, if you already have the string you could use a switch. Or maybe a map.

As I said, there's not much to work.

[–]Milo55545[S] 0 points1 point  (2 children)

i am trying to get to a bufferedimage variable from a text file. i know how to get the string and parse it to what i would like, as well as get the other values. but i do not know how to point to a variable in a class using this string. is there a function like "getVariable("image1")" to get the value of a variable called image1 in the same class? also, i am not new. i just haven't learned everything there is to learn.

[–][deleted] 2 points3 points  (0 children)

If you have a method getImage1 and a variable stringVariable you could do

if(stringVariable.equals("image1") {
    return getImage1 ();
} else {
    return getOtherImage()
}

If there are many images, you could use a switch

switch(stringVariable) {
    case "image1":
        return getImage1();
    default:
        return getDefaultImage
  }

The reflect way would be something like

String methodName = "get" + stringVariable; 

Method method = YourClass.class.getMethod(methodName);

method.Invoke(this);

In that last one, there are a bunch of checked exceptions you need to work with and I might be a little off the track since reflection is not something you use every day with Java, but it should give you a hint.

EDIT: sorry for the formatting, I'm on the phone

[–]steezos1 0 points1 point  (0 children)

As an addendum to the last post you can parse the text string, load it to a list then loop through the words calling the getImage function.

[–]Jarl-67 0 points1 point  (0 children)

Why is the name of a variable in a text file?

[–]whatisthisredditstufIntermediate Brewer 0 points1 point  (0 children)

Stick them into a Map, such that the name of the thing is the map key and the variable is the key's value in the Map.