-1

As far as I've researched, there's no way to match a String with a variable. probably I'm using the wrong word, here's what I mean by matching:
String grade="a";
double a=4.0;
And there's no way to associate the value of String grade with double a.

Similarly, what I want to do is associating value of a String with a method. Maybe I'm on the wrong track, let me briefly explain what I'm trying to achieve:
In the class player, there's a String name() method that returns This.name. There's no graphical design, and the only way for user to communicate with the program is typing. Basically, when person types name, I want name method to be ran. I'm pretty new, and the only way I can think of doing it is using a bunch of if statements, and adding another if statement for each method I add does not sound right to me.

Note: The reason I need String to be associated is because I'm going to use javax.swing.JTextArea to get input from the user, which returns String.

Thanks in advance

Burak
  • 73
  • 6
  • You can via [reflection](http://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string) but your proposed method is probably sufficient for your needs. – Zong Apr 23 '16 at 23:09
  • 1
    Reading from your question, it appears you want to call a method based on its name, that will be input by the user. In that case, you'll want to read about the Reflection API http://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string – Tunaki Apr 23 '16 at 23:10
  • 1
    You could use a good old switch – Bálint Apr 23 '16 at 23:15
  • 1
    Since no one mentioned it, I'll say it: this probably isn't the best setup. What if you want to rename a function in the future? I'd store a name in a dictionary/map, and map each name to a function. Relying on reflection for something like this seems a little heavy and unnecessary. – Carcigenicate Apr 23 '16 at 23:36
  • By using reflection you can match the string input to the method names to invoke the method: https://docs.oracle.com/javase/tutorial/reflect/member/methodInvocation.html – Herter Apr 25 '16 at 11:51
  • Thanks to all of you. Although I haven't tried it yet, it's exactly what I was looking for. – Burak Apr 25 '16 at 17:31

3 Answers3

0

Java is not a dynamic interpreted language like Python or Perl.

To associate arbitrary strings with values you should use a Map<String,ValueClass> where ValueClass is the value to associate, such as Integer, Float, BigDecimal or your own value class.

Jim Garrison
  • 81,234
  • 19
  • 144
  • 183
0

Yes. It's called a Map.

Here's some sample code of how to use one:

Map<String, Double> map = new HashMap<>();

map.put("grade", 4.0);

double a = map.get("grade");

If you want to store a variety of value types use Map<String, Object>, but you'll have to make unsafe casts when retrieving and using the values returned from get().

Bohemian
  • 365,064
  • 84
  • 522
  • 658
0

Because grades are usually fixed from A to F, I would use an enum to map each grade to a numeric value:

enum Grade {
    A(4.0), B(3.0) // etc...

    private double val;

    private Grade(double val) {
        this.val = val;
    }

    public double getVal() {
        return val;
    }
}

Then use Grade.A.getVal() when you need the numeric value of the A grade.