aspose file tools*
The moose likes Beginning Java and the fly likes passing values from one method to other Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "passing values from one method to other" Watch "passing values from one method to other" New topic
Author

passing values from one method to other

adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Hi
i have two different classes. One for adding 2 nums and other to subtract. i used BufferedReader for user input. My requirement is based on the input given the program must call the method. For example: "add 1 2". This line contains add so the program must pass the two values i.e 1 and 2 to add method. If the line contains sub then the values must be passed to sub method. Can anybody please help me out. I have already diff classes to perform that operations, they are here.

The add class.


The sub class.
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
You can do it all in one class (although you could keep the add/sub parts in separate classes if you wish).

Your code isn't far away, you just need to think a bit more about how you structure it.

What code makes the decision whether to call add or sub?
Is this the same code that gets the user's input?
How many classes do you actually need? One, Two or Three?

Without knowing how complex your final program needs to be, I would say you could have one class that gets the user input in its "main" method, parses it and then calls add/sub as needed.
Like I said - you're nearly there.


SCJP6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
If you are supposed to be learning object-oriented programming, don't do everything in one class. And don't do lots in the main method. What you need is a readFromKeyboard method. Then call add(int i, int j) subtract(int i, int j) multiply(int i, int j) and divide(int i, int j) methods. These latter methods should be in a different class, and can probably be declared as static, if they return their result.
bhavesh bhatnagar
Greenhorn

Joined: Jun 15, 2009
Posts: 26
You have to create a Main class
and call add and sub class from it.

You can do it like this
from command prompt run

>main add 5 6


In Main class type

# BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
# int a=Integer.parseInt(br.readLine());
# int b=Integer.parseInt(br.readLine());



and on main make comparisions like
if(args[0]=="add")
call add class's method and pass a & b to it
else if(args[0]=="sub")
call sub class's method and pass a & b to it

i think this way your problem got solved

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
bhavesh bhatnagar wrote:
and on main make comparisions like

i think this way your problem got solved


You probably don't want to recommend that the original poster compare Strings with == since this doesn't check that two strings contain the same letters but instead checks if they are the exact same object.

Instead compare Strings with a call to the equals(...) method or equalsIgnoreCase(...).
bhavesh bhatnagar
Greenhorn

Joined: Jun 15, 2009
Posts: 26
thanks pete

i forgetted that method

You should use equals() instead of "=="
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Hi all
and thanks for your replies.
I have tried doing like this

code: for main class


code: for required methods(calc class)



I needed to give the whole output in a single line. How can i achive it.

Thanks and Regards
alexander
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
For single-line output, have a look at System.out, which is a PrintStream, and look through its methods.
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Sorry guyz. My question was wrong. I need to give the total input in a single line. As per the above program the user has to enter first the operation i.e add,sub or mul, the the 2 numbers are entered in two different lines. So instead of all that i wan to give the input in a single line just like this : add 1 2

How can i do that.


Thanks and Regards
alexander
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 559
    
    7

In your code see readline (this will read an entire line) where you assume there will be a string with an operation and then read two different lines with int's - so you will need to change that - what if the first read, read in the operator and the two ints into a string and you parsed that? IE: split on a space character.



adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Hi.
i know that i can split and do and all those operations. But isn't there any other way or method to full fill my need.



Thanks and Regards
alexander
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing values from one method to other