I’m a Java newbie, and this is a real basic, basic question, so please don’t
miss the tree for the forest.
I just need to know how to access the data coming back from the method
private static int toInt( String number )
{
int multiplicand = 0;
// do stuff to figure out a multiplicand
return multiplicand;
}
public static void main( String[ ] args )
{
int anotherVariable = 53;
toInt( args[ 0 ] );
So, what do I say here after I’ve come back from the toInt method in order to access what is in multiplicand?
Lets say I want to multiply what came back in multiplicand * anotherVariable.
I cannot use multiplicand because the editor just says “cannot resolve multiplcand to a variable.”Thank you in advance to all the great programmers with the patience to tolerate us newbies.
you assign the result of the function invocation to a variable, and then use that.
int result = toInt ("1");You can now use
resultas you see fit; it contains whatever you returned in the method, which was the value ofmultiplicand. So in your case, you can now doint anotherResult = result * anotehrVariable;Note you can also do
int anotherResult = toInt(args[0]) * anotherVariable;without assigning the result of your invocation of
toIntto an intermediary variable.I prefer to assign function invocations to variables; makes debugging easier. If you want to use the result more than once, it’s almost always better to use a variable.