Results 1 to 8 of 8
  1. #1
    user07 is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default operators (/,*,+,-)

    this is a pretty simple question but i am not getting the required output..

    Write a program that accepts two numbers and a operator like (+,-,*, /) as
    command line arguments and perform the appropriate operation indicated by operator ??
    here is my code
    Java Code:
    class Operation
    {
    	public static void main (String args[])
    	{
    		int res;
    		int a=Integer.parseInt(args[0]);
    		int b=Integer.parseInt(args[1]);
    		char c=args[2].charAt(0);
    		System.out.println("char: "+ c);
    
    		switch(c)
    		{
    			case '+': res=a+b; System.out.println("Sum : "+ res);
    			break;
    			case '-': res = a-b; System.out.println("difference : "+ res);
    			break;
    			case '*': res = a*b; System.out.println("product : "+ res);
    			break;
    			case '/': res = a/b; System.out.println("quotient : "+ res);
    			break;
    			default : System.out.println("Sorry invalid operand entered");
    		}
    	}
    }
    the problem is +,/, and - are working fine but * is displaying Invalid operand (ie default statement ).
    output demo :

    D:\>java Operation 6 3 -
    char: -
    difference : 3

    D:\>java Operation 6 3 +
    char: +
    Sum : 9

    D:\>java Operation 6 3 /
    char: /
    quotient : 2

    D:\>java Operation 6 3 *
    char: $
    Sorry invalid operand entered

    D:\>
    Everything is working fine except for multiplication * .. plz help

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: operators (/,*,+,-)

    Java Code:
    char c=args[2].charAt(0);
    System.out.println("char: "+ c);
    It looks like args[2] doesn't contain what you think it does. In particular what it contains seems to start with a $. It might shed light on the problem to print the whole args[2] string rather than just looking at its first character.

    [Edit] While you're at it, try printing all of the arguments. Something like this:

    Java Code:
    public class Star {
            // java Star *
        public static void main(String[] args) {
            System.out.println("argument length = " + args.length);
            for(int i = 0; i < args.length; i++) {
                System.out.println(args[i]);
            }
        }
    }
    Last edited by pbrockway2; 09-02-2012 at 10:49 AM.

  3. #3
    user07 is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Re: operators (/,*,+,-)

    this the the only reason i included the following line in my code :
    System.out.println("char: "+ c);
    but the problem is the program is working fine for all other characters + , / , and - but i can't understand why is * giving the problem...

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: operators (/,*,+,-)

    Did you print all the arguments as suggested?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    user07 is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Re: operators (/,*,+,-)

    yes , they worked fine for +,/,- but in * it is displaying all java and classes files present in the folder.. is it because * is a wildcard??
    please suggest me the correct way to do this ...

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: operators (/,*,+,-)

    Yep.
    That's the windows command line translating the '*' into a directory listing, because it's a wildcard.
    You might need to stick the parameters in quotes, then split them in the code.
    Please do not ask for code as refusal often offends.

  7. #7
    user07 is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Re: operators (/,*,+,-)

    k.. thanks :)

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: operators (/,*,+,-)

    Quote Originally Posted by Tolls View Post
    Yep.
    That's the windows command line translating the '*' into a directory listing, because it's a wildcard.
    You might need to stick the parameters in quotes, then split them in the code.
    I thought so too till I read this bug report:
    Bug ID: 5036373 Tool doc for 'java' should document Windows-specific command arg processing

    It's not the command line that expands the wildcard, it's the Java executable.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Logical Operators
    By Everyman in forum New To Java
    Replies: 9
    Last Post: 03-26-2012, 03:56 PM
  2. >>> and >> Logical operators
    By Playagood in forum New To Java
    Replies: 3
    Last Post: 02-19-2012, 04:07 AM
  3. string operators
    By cagipple in forum New To Java
    Replies: 5
    Last Post: 04-17-2011, 01:04 AM
  4. where exactly bit shift operators are useful?
    By sandeepsai39 in forum New To Java
    Replies: 6
    Last Post: 09-08-2010, 11:58 AM
  5. New to operators, need some help...
    By Stryker4526 in forum New To Java
    Replies: 4
    Last Post: 09-26-2009, 01:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •