• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

function call

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question when I am trying to write a calculator rogram. I want to get input from command line, example 4+6 or 5*2. how can I read this input and make it call my method calculator(num1, "op", num2).
thank you
haijun
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The args are the expression you put in at the command line:
public static void main(String[] args ) {
if ( args.length == 0 ) {
System.out.println("Missing Argument\n");
} else {
String expressionToCalculated = args;
}
Once you have the whole expression in the String, you can do the following:
calculate(expressionToCalculated[0],expressionToCalculated[1],expressionToCalculated[2]);
Of course, in a more scalable program where you'd have more than 2 numbers and 1 operator, you'd have to think about order of operation.
Shama

[This message has been edited by Shama Khan (edited March 08, 2001).]
 
haijun wang
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your help. I still have question about this problem. I think I need put space between the operand and operator. If I do'nt, the compiler will thought it a string. How can I get this function working without putting space there.
Thanks
Haijun
 
Shama Khan
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would agree that the best way is to not limit the user input
(forcing them to put a space). So let's assume you may or may not have a space. You would copy args into a String array and
build a new expression by evaluating each character to differentiate between operators and operands.
I may have done this program long time ago using C++ but in Java
it should be a lot less tedious.
Shama
 
haijun wang
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Shama:
Thank you for your help. One thing I am still comfucing is that when I input the expression, the input may be considered as string not integer. Last night, I was trying to make my program work. But I failed. Please give me some help to figure why my code can not be compiled.
Thank you.
HAIJUNpublic class Evaluator
{
public static void main(String [] args)
{
if (args.length == 0)
System.out.println("Missing Argument\n");
else
calculate(args[0], args[1], args[2]);
System.out.println("The answer is " + answers);
int calculate(int num1, String op, int num2)
{
int answers = 0;
if (op == "+")
{
answers = num1 + num2;
}
else if(op == "-")
{
answers = num1 - num2;
}
else if(op == "/")
{
if(num2 == 0)
{
display.setText("ERROR");
}
else
answers = num1 / num2;
}
else if(op == "*")
{
answers = num1 * num2;
}
return answers;
}//close calculate
}// close main
}// close class
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All input from the command line is a string, so "java Calculate 3+2" has an args.length == 1 and arg[0] = "3+2". arg[1] and arg[2] are undefined (array index out of bounds.)
You'll probably want some intermediate step(s) that will take 3+2 and parse it into 3, +, and 2. I'm still beginning with java, but you will want to parse the input string(s) into indivual strings of operators and operands. I don't know the method that will do this, you'll have to hunt around.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
You can use indexOf in the String class to find characters to determine what the math function is that is required to perform. Then you bust up the string using substring to get the diffrents numbers.
In the section where I do the conditions to determine what the mathOperator is, you can just replace all this with a method call (and make a method) that would look something like this.
doCalculation(firstNum, mathOperator, secondNum);
Ex.

Here's a problem I haven't tried to address with this solution.. What if you want to use negative numbers? Like (-3)*(5). How can it determine the diffrence beterrn the - in -3 and the *5.
That will be a solution for another post.


[This message has been edited by John Bateman (edited March 09, 2001).]
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringTokenizer may provide a cleaner implementation.

 
haijun wang
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Bateman:
Did you compile and run the program you posted here. I tried last night. It looks like there is small problems in it. It can not pass compilation.
Thank you again.
Haijun
 
John Bateman
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Carl Trusiak:
[B]StringTokenizer may provide a cleaner implementation.
[/B]


Hi
You're absolutely right, StringTokenizer should work nicely here (better in fact cause it's cleaner), I just had no idea you could give a list of potential seperators.
I learned something new today.
 
John Bateman
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by haijun wang:
Dear Bateman:
Did you compile and run the program you posted here. I tried last night. It looks like there is small problems in it. It can not pass compilation.
Thank you again.
Haijun


Hi
I tried it again and it compiles and runs over here. Can you give me the error you are getting?
Thanks.

 
haijun wang
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Mr. Bateman:
I have compiled and run your program. I did learn a lot from your program. Thank you very much. All of my questions have been cleared about this problem.
Haijun.
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic