473,320 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.length;i++)
{

multiply=multiply*Integer.parseInt(args[i]);
sum=sum+Integer.parseInt(args[i]);
// minus=minus-Integer.parseInt(args[i]);
}

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


}

}
Jun 22 '07 #1
3 2009
r035198x
13,262 8TB
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.length;i++)
{

multiply=multiply*Integer.parseInt(args[i]);
sum=sum+Integer.parseInt(args[i]);
// minus=minus-Integer.parseInt(args[i]);
}

//System.out.println("the minus is"+minus);
System.out.println("The product is " + multiply);
System.out.println("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?
Jun 22 '07 #2
JosAH
11,448 Expert 8TB
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.length;i++)
{

multiply=multiply*Integer.parseInt(args[i]);
sum=sum+Integer.parseInt(args[i]);
// minus=minus-Integer.parseInt(args[i]);
}

//System.out.println("the minus is"+minus);
System.out.println("The product is " + multiply);
System.out.println("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:

Expand|Select|Wrap|Line Numbers
  1. for (int i= 0; i < args.length; i++) ...
  2.  

kind regards,

Jos
Jun 22 '07 #3
Hi,

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

minus=Integer.parseInt(args[0])-Integer.parseInt(args[1]);

For the division also u need to use as above.


cheers,
Sateesh
Jun 26 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
21
by: MOvetsky | last post by:
Is the following code ISO C++ standard compliant? If yes, is it guaranteed that it will not crash on compliant platforms? If yes, will it print "Pointers are equal" on any compliant platform? Will...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
5
by: jerry | last post by:
I need to modify the code of a command-line tool. The source code starts out like this: int main(int argc, char *argv) { int ch, A, B ; while ((ch = getopt(argc, argv, "AB")) != -1)...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.