0

In Matlab, whenever I enter a variable name into command window, it displays the value of that variable so far, very useful feature. So that, I tried to add the same feature in java. That is, I want to display the value of any kind of variable(int, double, value of array such as signal[4] ) when the user enters something. If entered variable does not exist, it gives error or blows up. More precisely, I want to add something similar to the code below at the end of my project.

while(true){
        Scanner scan = new Scanner(System.in);
        System.out.println("Yep? : ");
        String s = scan.nextLine();
        System.out.println(s + " = value of that entered variable");
        }

Obviously, It is not as easy as it sounds but that is the reason I am asking in here. Is there any easy way to add this simplicity?

Tim
  • 1

1 Answers1

0

No. For java, this is pretty meaningless, since the

  • The scope is the one of your loop
  • it could be at best applied to the names of fields via the reflection API
  • but for local variables, the name is lost during compilation (if the compiler likes to optimize, it could remove the variable completely).

I could imagine that upon binding a reasonably chosen set of objects into an Java Expression Language (http://docs.oracle.com/javaee/6/tutorial/doc/bnahq.html) - using a variable mapper (see JSTL/JSP EL (Expression Language) in a non JSP (standalone) context) you could achieve something what you in a simple way.

Community
  • 1
  • 1
Alexander Kemp
  • 202
  • 1
  • 10