Getting a function to return two integers

18,008

Solution 1

You could have the method return an array of int:

public static int[] calc (int s, int b, int c, int d, int g)

Solution 2

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;
    }
}

Solution 3

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.

Solution 4

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");
}
Share:
18,008
Spencer
Author by

Spencer

Updated on June 13, 2022

Comments

  • Spencer
    Spencer almost 2 years

    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");                    
        }
    
  • strager
    strager over 13 years
    This doesn't handle the case when only s is returned.
  • Stephen C
    Stephen C over 13 years
    Also, note that in the "no answer" case, the method has to return an empty int[].
  • Gabe
    Gabe over 13 years
    "has to"? Can't it return null or throw an exception?
  • Gabe
    Gabe over 13 years
    strager is right; you should be using Integer instead of int so that they can be null.
  • Stephen C
    Stephen C over 13 years
    @Gabe - true - but then why would he call println?
  • Gabe
    Gabe over 13 years
    Stephen: he'd call println probably for the same reason he'd return s && g -- he just didn't know the right way to handle it.
  • Gabe
    Gabe over 13 years
    What do T and Y stand for?
  • TheSpy
    TheSpy over 13 years
    T and Y are Generic placeholders for specific types. In the OPs case they both would be Integer
  • TheSpy
    TheSpy over 13 years
    @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.
  • TheSpy
    TheSpy over 13 years
    String would be the worst solution I could think of. The already mentioned Pair and int[] are the apropriate solutions to the OPs problem.
  • Gabe
    Gabe over 13 years
    Johannes Wachter: If it's just a best practice, then Stephen ought to have put "should" in bold instead of "has to".
  • Gabe
    Gabe over 13 years
    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?
  • Jes
    Jes over 13 years
    Edited to use Integer. You are both right, I missed that case.
  • Jaswinder Singh
    Jaswinder Singh almost 3 years
    As per question, he wants to return two numbers, which is possible by using Array as a return type. Your solution is returning the sum of two numbers. Appreciating efforts.