I don't know any Java, but would it be possible to put a and b into an array, then return the array?
To my knowledge, you can't. At least not in one method. This works the same in C and C++, but the bigger question is to what purpose do you want to return two values at the same time?
I don't know any Java, but would it be possible to put a and b into an array, then return the array?
Code:class retarr { // one of the most important features of Java is its access modifiers. // The 'static' modifier differs from that of C and C++ // in that it tells the compiler that this method cannot be instantiated, // but rather is 'static' to this application. public static int[] retInt(int a, int b) { int[] intArr = new int[2]; // there are many ways to create arrays in Java, this is one of them. intArr[0] = a; intArr[1] = b; return intArr; } // this is your garden variety main method public static void main(String args[]) { int[] intArr2 = retInt(3,6); for (int i=0; i<intArr2.length; i++) System.out.println(intArr2[i]); } }
As such, objects in C++ can't return stuff, that's functionally which lies in the implementation of the member functions. I assume that it's the same in that coffee language.
An array won't do it, since I'm trying to return different variables, a float and an String.
Is there another way of passing values from a method to the caller? The method I'm working on generates an int and a String that needs to be passed on to the caller.
Why dont you create a and b as global variable to expand its scoope?
public class test {
float a;
int b;
public float test() {
a = 1.1;
b = 1;
}
This way you may have the values available to all the methods, but i may be wrong. ;D
Yes, I've thought about using global variables, but I'm trying to avoid that as mush as I can. If to program gets big, it's hard to keep track of them (done that in Perl, became a nightmare).Why dont you create a and b as global variable to expand its scoope?
public class test {
* float a;
* int b;
* public float test() {
* * * a = 1.1;
* * * b = 1;
}
This way you may have the values available to all the methods, but i may be wrong. ;D
What kind of values are you going to return?, are they string,int,etc.? some example of the data.
Because the array solution can be used if we know some of this answers.
What I'm trying to do is to have a method that does a query in a database and return the number of rows (int) that matched the query. If something goes wrong with the query (couldn't connect to the database, etc.), I want the method to return a String with an error message, otherwise an empty string.What kind of values are you going to return?, are they string,int,etc.? some example of the data.
Because the array solution can be used if we know some of this answers.
Ok this i what i'll do.
1 the function will only return 1 value at time.
2 the value is is a number can be returned as a string and the can be casted.
3 If the value is an error message its going to be a string.
So the function for me can return a string.
public class test {
public String test() {
.
.
.
return a;
}
Remember If you are returning Records number dont use int, use long.
In java to convert a string to Long use: parseLong. if the value cant be parsed (error message string) it throws a NumberFormatException Exception.
HtH
Bookmarks