Passing arithmetic expression through command line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imtiazalikhan
    New Member
    • Jun 2007
    • 3

    Passing arithmetic expression through command line

    hi i am trying to passing arthematic expressions through command line arguments but i only get result of addition and multiplication cant get result of
    subtraction and division:

    //c:\jdk1.6.0\bin \javac mentalmath
    //compilation is successful
    c:\jdk1.6.0\bin \java mentalmath 12 6
    **result** 72 18 // only give result of multiplication and addition of above numbers. but not give result of subtraction or divsion.

    Code:
    class mentalmath
    {
    public static void main(String args[])
    {

    int multiply= 1;
    int sum=0;
    // int minus=0;
    for(int i=1;i<=args.len gth;i++)
    {

    multiply=multip ly*Integer.pars eInt(args[i]);
    sum=sum+Integer .parseInt(args[i]);
    // minus=minus-Integer.parseIn t(args[i]);
    }

    //System.out.prin tln("the minus is"+minus);
    System.out.prin tln("The product is " + multiply);
    System.out.prin tln("the sum is"+sum);


    }

    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by imtiazalikhan
    hi i am trying to passing arthematic expressions through command line arguments but i only get result of addition and multiplication cant get result of
    subtraction and division:

    //c:\jdk1.6.0\bin \javac mentalmath
    //compilation is successful
    c:\jdk1.6.0\bin \java mentalmath 12 6
    **result** 72 18 // only give result of multiplication and addition of above numbers. but not give result of subtraction or divsion.

    Code:
    class mentalmath
    {
    public static void main(String args[])
    {

    int multiply= 1;
    int sum=0;
    // int minus=0;
    for(int i=1;i<=args.len gth;i++)
    {

    multiply=multip ly*Integer.pars eInt(args[i]);
    sum=sum+Integer .parseInt(args[i]);
    // minus=minus-Integer.parseIn t(args[i]);
    }

    //System.out.prin tln("the minus is"+minus);
    System.out.prin tln("The product is " + multiply);
    System.out.prin tln("the sum is"+sum);


    }

    }
    1.) Code tags code tags code tags code tags code tags code tags code tags
    2.) Where in your code did you instruct the program to subtract or divide?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by imtiazalikhan
      hi i am trying to passing arthematic expressions through command line arguments but i only get result of addition and multiplication cant get result of
      subtraction and division:

      //c:\jdk1.6.0\bin \javac mentalmath
      //compilation is successful
      c:\jdk1.6.0\bin \java mentalmath 12 6
      **result** 72 18 // only give result of multiplication and addition of above numbers. but not give result of subtraction or divsion.

      Code:
      class mentalmath
      {
      public static void main(String args[])
      {

      int multiply= 1;
      int sum=0;
      // int minus=0;
      for(int i=1;i<=args.len gth;i++)
      {

      multiply=multip ly*Integer.pars eInt(args[i]);
      sum=sum+Integer .parseInt(args[i]);
      // minus=minus-Integer.parseIn t(args[i]);
      }

      //System.out.prin tln("the minus is"+minus);
      System.out.prin tln("The product is " + multiply);
      System.out.prin tln("the sum is"+sum);


      }

      }
      You should start your loop at i= 0, not i= 1 and you should stop one loop pass
      earlier; like this:

      [code=java]
      for (int i= 0; i < args.length; i++) ...
      [/code]


      kind regards,

      Jos

      Comment

      • sateesht
        New Member
        • Apr 2007
        • 41

        #4
        Hi,

        if you want to get the result for minus, u need to use below code:

        minus=Integer.p arseInt(args[0])-Integer.parseIn t(args[1]);

        For the division also u need to use as above.


        cheers,
        Sateesh

        Comment

        Working...