views:

185

answers:

5

I am writing a function and I want it two return two integers as results. However, I cannot get it to do this. Could someone help me? Here is my best shot

public static int calc (int s, int b, int c, int d, int g)
    {
        if (s==g)
            return s;
        else if (s+b==g)
                return s && b;

        else if (s + c==g)
                return s && c;

         else if (s+d==g)
                return s && d;

        else
            System.out.println("No Answer");                    
    }
+5  A: 

You could have the method return an array of int:

public static int[] calc (int s, int b, int c, int d, int g)
akf
Also, note that in the "no answer" case, the method **has to** return an empty `int[]`.
Stephen C
"has to"? Can't it return null or throw an exception?
Gabe
@Gabe - true - but then why would he call println?
Stephen C
Gabe
@Gabe when returning 'no result' with collections/arrays it's a best practice to return an empty collection/array since it avoids to have null checks everywhere. You can then handle the returnvalue based on its size/length in a more robust way.
Johannes Wachter
Johannes Wachter: If it's just a best practice, then Stephen ought to have put "should" in bold instead of "has to".
Gabe
A: 

why do you want to do this? and if you have some need like this can't you change your return type to string, because in case of string you can have separator between two values which will help you in extracting values.... say 10&30 ,

I agree this is a wrong way of solving...i assumed that there is limitation of sticking to primitive datatype

Saket Bansal
-1 abuse of Object types. Use a Pair instead
bwawok
`String` would be the worst solution I could think of. The already mentioned `Pair` and `int[]` are the apropriate solutions to the OPs problem.
Johannes Wachter
+3  A: 

Make a small inner class that has two integers.

private static class TwoNumbers {
    private Integer a;
    private Integer b;

    private TwoNumbers(Integer a, Integer b) {
        this.a = a;
        this.b = b;
    }
}

You create a instance of the class and return that instead.

Jes
This doesn't handle the case when only `s` is returned.
strager
strager is right; you should be using `Integer` instead of `int` so that they can be null.
Gabe
Edited to use Integer. You are both right, I missed that case.
Jes
+3  A: 

Make a "pair" class and return it.

public class Pair<T,Y>
{
    public T first;
    public Y second;
    public Pair(T f, Y s)
    {
        first = f;
        second = s;
    }
}
JH
What do `T` and `Y` stand for?
Gabe
`T` and `Y` are Generic placeholders for specific types. In the OPs case they both would be `Integer`
Johannes Wachter
`T` stands for "type", `V` stands for "value", `E` stands for "element", but I have no idea what `Y` stands for, and am unsure of that `T` stands for in the context of `Y`. Are they just randomly chosen letters, or do they stand for some longer words?
Gabe
+2  A: 

For this specific problem, since the answer always returns s:

....
    return s;
....
    return s && b;
....
    return s && c;
....
    return s && d;
....

you could just return the 2nd value. I use 0 to indicate "just s" since the first case (if (s==g)) could be thought of as if (s+0==g). Use a different sentinel value than 0 for this, if necessary.

public static int calc (int s, int b, int c, int d, int g)
{
    if (s==g)
        return 0;
    else if (s+b==g)
            return b;
    else if (s+c==g)
            return c;
     else if (s+d==g)
            return d;
    else {
        // System.out.println("No Answer");

        // Probably better to throw or return a sentinel value of
        // some type rather than print to screen.  Which way
        // probably depends on whether "no answer" is a normal
        // possible condition.
        throw new IndexOutOfBoundsException("No Answer");
    }
}

If no exception is thrown, then s is always the first result:

try {
    int result1 = s;
    int result2 = calc(s, b, c, d, g);
} catch (IndexOutOfBoundsException ex) {
    System.out.println("No Answer");
}
Bert F