| | |
value of a variable whoose name is in another string variable...
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 27
Reputation:
Solved Threads: 0
Hi I want to be able to find out the int (or float) value of a variable whoose name is in another string variable
I want 10 to be printed but all I get is :
Can you help me?
•
•
•
•
int x = 10;
String var = "x";
System.out.println(Integer.valueOf(var).intValue());
•
•
•
•
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)
Last edited by Spartan552; Apr 7th, 2008 at 10:21 pm.
•
•
Join Date: Jan 2008
Posts: 27
Reputation:
Solved Threads: 0
from http://forum.java.sun.com/thread.jsp...sageID=9536652
Output :
but is it possible to do that without using a script language?
•
•
•
•
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();
}
}
}
•
•
•
•
n=10; n+4 = 14.0
Last edited by Spartan552; Apr 7th, 2008 at 10:47 pm.
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
----------------------------------------------
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
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.
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
----------------------------------------------
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
![]() |
Other Threads in the Java Forum
- Previous Thread: Project help
- Next Thread: Hiding Min & Max button in JFrame
| Thread Tools | Search this Thread |
6 @param actuate android api applet application arc array arrays automation balls binary bluetooth bold business byte c++ chat class client code codesnippet collections compare component coordinates database defaultmethod detection doctype dragging ebook eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework hql html ide ideas image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans newbie nextline parameter php pong problem program programming project recursion recursive scanner sell server set sms sort sql string sun swing swt terminal threads tree web websites windows






