I want to do something very easy
1+2=3
String var1 = "1";
String var2 = "2";
String var3 = var1 + var2;
The problem: var3 is 12
I want to do something very easy
1+2=3
String var1 = "1";
String var2 = "2";
String var3 = var1 + var2;
The problem: var3 is 12
String var1 = "1";
String var2 = "2";
String var3 = String.valueOf(Integer.parseInt(var1) + Integer.parseInt(var2));
You must know that var1
and var2
are Strings so you have to convert them to number type like int so you can add them together because when you add to String together you perform concatenation .
First approach is using Integer.valueOf(String)
that returns new Integer()
Second approach is using Integer.parseInt(String)
that returns primitives int
Code:
String var1 = "1";
String var2 = "2";
int var3 = Integer.parseInt(var1) + Integer.parseInt(var2);
System.out.println(var3);
Extra info using eval
function :
public static void main(String[] args) throws ScriptException {
String var1 = "1";
String var2 = "2";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Object result = engine.eval(var1.concat("+").concat(var2));
System.out.println(result.toString());
}
Source for extra part: Is there an eval() function in Java?
Result:
3
The previous answer already kind of answers the problem by providing (in my opinion suboptimal) code, but I want to provide a better way with an explanation.
You want to get the sum of two numbers (two decimals (1 and 2)).
String var1 = "1";
String var2 = "2";
String var3 = var1 + var2;
Value of var3: 12
.
This line
String var3 = var1 + var2;
will be compiled to
String var3 = new StringBuilder(var1).append(var2).toString();
Now it is probably more obious what this code is doing, it's concatenating (not adding the numbers contained) two String
s ("1" and "2"), which yields to the result "12". A String
is not a number (it can be, but in "String
-format", not as a ready-to-use number) and should not be treated as such.
A simple solution would be using the primitive data type int
(integer, 4 bytes long).
Something like this:
int var1 = 1;
int var2 = 2;
int var3 = var1 + var2;
Value of var3: 3
.
In your case, you are creating a calculator, and, to extend my previous point, even there you should avoid using String
s to store your calculations parameters and results, for multiple reasons:
You will have to convert a number to a String and String to number every time you use them.
It's slower. Using primitive types is and will always be faster than using for example the wrapper class for it (e.g.: Integer
for primtive int
) or any other class.
It's hard to read and to maintain. If you fetch input from the keyboard you might have to parse the given input to primitives you can work with. You can do this in the beginning and it will make your life much easier.
Also, var1
, var2
and var3
are horrible parameter names. Something like firstNumber
, secondNumber
and result
would in your case be more fitting. You should always name your variables with care and to make them self-explainatory.
I strongly recommend you to read a basic Java tutorial, like this: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html.