I am trying to make a command line calculator using Java, I have come up with the code below it works without the if-else
ladder,but I can't make it work with if-else
, during debugging I tried printing the "z" which is indeed add when the arguments are passed as java name 1 2 add
, But I can't seem to trigger the id (z == "add")
, please suggest what I am missing.
public class commandlinecal {
public static void main(String arg[])
{
int x = Integer.parseInt(arg[0]);
int y = Integer.parseInt(arg[1]);
String z = arg[2];
if (z == "add")
{
add(x,y);
}
else if (z == "sub")
{
sub(x,y);
}
else if (z=="mul")
{
mul(x,y);
}
else if(z=="div")
{
div(x,y);
}
}
public static void add(int x,int y)
{
int result = x + y;
System.out.println("The sum is" + " "+result);
}
public static void sub (int x,int y)
{
int result = x-y;
System.out.println("The sub is"+" "+result);
}
public static void mul (int x,int y)
{
int result = x * y;
System.out.println("The multiplication is"+" "+result );
}
public static void div (int x,int y)
{
float result = x / y;
System.out.println("The division is"+" "+result);
}
}