value of a variable whoose name is in another string variable...

Thread Solved

Join Date: Jan 2008
Posts: 27
Reputation: Spartan552 is an unknown quantity at this point 
Solved Threads: 0
Spartan552 Spartan552 is offline Offline
Light Poster

value of a variable whoose name is in another string variable...

 
0
  #1
Apr 7th, 2008
Hi I want to be able to find out the int (or float) value of a variable whoose name is in another string variable


int x = 10;
String var = "x";
System.out.println(Integer.valueOf(var).intValue());
I want 10 to be printed but all I get is :

Exception in thread "main" java.lang.NumberFormatException: For input string: "x"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.valueOf(Integer.java:553)
at Line2f.main(Line2f.java:57)
Can you help me?
Last edited by Spartan552; Apr 7th, 2008 at 10:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 27
Reputation: Spartan552 is an unknown quantity at this point 
Solved Threads: 0
Spartan552 Spartan552 is offline Offline
Light Poster

Re: value of a variable whoose name is in another string variable...

 
0
  #2
Apr 7th, 2008
from http://forum.java.sun.com/thread.jsp...sageID=9536652

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class ScriptDemo {

public static void main(String[] args) {

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");

try {
String expression = "n=10; n+4";
Object result = engine.eval(expression);
System.out.println(expression+" = "+result);
} catch(ScriptException se) {
se.printStackTrace();
}
}
}
Output :
n=10; n+4 = 14.0
but is it possible to do that without using a script language?
Last edited by Spartan552; Apr 7th, 2008 at 10:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,358
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: value of a variable whoose name is in another string variable...

 
0
  #3
Apr 8th, 2008
Use a map.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 27
Reputation: Spartan552 is an unknown quantity at this point 
Solved Threads: 0
Spartan552 Spartan552 is offline Offline
Light Poster

Re: value of a variable whoose name is in another string variable...

 
0
  #4
Apr 8th, 2008
what do you mean by map?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,358
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: value of a variable whoose name is in another string variable...

 
0
  #5
Apr 8th, 2008
http://java.sun.com/javase/6/docs/ap...l/HashMap.html

Edit: P.S. The API docs are a wonderful thing. They answer all sorts of questions.
Last edited by masijade; Apr 8th, 2008 at 2:41 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC