File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes function call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "function call" Watch "function call" New topic
Author

function call

haijun wang
Greenhorn

Joined: Aug 18, 2000
Posts: 21
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
Shama Khan
Ranch Hand

Joined: Aug 14, 2000
Posts: 185
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).]


Shama
haijun wang
Greenhorn

Joined: Aug 18, 2000
Posts: 21
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

Joined: Aug 14, 2000
Posts: 185
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

Joined: Aug 18, 2000
Posts: 21
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
Bruce Wingate
Ranch Hand

Joined: Feb 16, 2001
Posts: 32
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.
John Bateman
Ranch Hand

Joined: Mar 09, 2000
Posts: 320
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).]


SOURCE CODE should be SURROUNDED by "code" tags.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
StringTokenizer may provide a cleaner implementation.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
haijun wang
Greenhorn

Joined: Aug 18, 2000
Posts: 21
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

Joined: Mar 09, 2000
Posts: 320
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

Joined: Mar 09, 2000
Posts: 320
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

Joined: Aug 18, 2000
Posts: 21
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: function call