Results 1 to 4 of 4
Thread: Help With Calculator Pl0x
- 03-06-2011, 06:39 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
Help With Calculator Pl0x
I am new to java, and am attempting to learn it. So far I am trying to make a simple command prompt argument calculator, and it isnt working.
Here's my code
public class calculator
{
public static void main( String[] args)
{
int i = Integer.parseInt(args[0]) ;
int y = Integer.parseInt(args[2]) ;
String modi =(args[1]) ;
int ans = 0;
if( args.length !=3)
{
System.out.println("wrong number of arguments") ;
return;
}
if (modi=="+")
{
ans =(i + y );
System.out.println(ans) ;
}
else if (modi=="-")
{
ans = (i - y );
System.out.println(ans ) ;
}
else if (modi=="/")
{
ans = (i / y );
System.out.println(ans ) ;
}
else if (modi=="x")
{
ans = (i*y );
System.out.println(ans ) ;
}
else
{
System.out.println("Failure In Arguments") ;
}
}
}
I'm trying to use 56 x 2 for the arguments.
My guess is that I'm missing something simple, but i can't find it.
I have tried sending the arguments through the command prompt and eclipse and neither work.
Any correction would be helpful.
-
For one, never use == to compare Strings but instead use equals(...) or equalsIgnoreCase(...).
- 03-06-2011, 06:43 PM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
else if (modi=="x") -->
else if (modi.equals("x"))
Java: String Comparison
- 03-06-2011, 06:51 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Calculator
By Moshe22 in forum New To JavaReplies: 8Last Post: 01-17-2011, 05:29 AM -
Help in a calculator
By Ayannie in forum New To JavaReplies: 6Last Post: 01-04-2011, 08:21 PM -
Calculator
By water in forum AWT / SwingReplies: 4Last Post: 09-23-2009, 06:00 AM -
help with calculator
By kalibballer in forum New To JavaReplies: 8Last Post: 04-01-2009, 12:57 PM -
Calculator help.
By madkidd02 in forum New To JavaReplies: 2Last Post: 10-25-2008, 07:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks