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");
}
Author by
Spencer
Updated on June 13, 2022Comments
-
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 over 13 yearsThis doesn't handle the case when only
s
is returned. -
Stephen C over 13 yearsAlso, note that in the "no answer" case, the method has to return an empty
int[]
. -
Gabe over 13 years"has to"? Can't it return null or throw an exception?
-
Gabe over 13 yearsstrager is right; you should be using
Integer
instead ofint
so that they can be null. -
Stephen C over 13 years@Gabe - true - but then why would he call println?
-
Gabe over 13 yearsStephen: he'd call
println
probably for the same reason he'd returns && g
-- he just didn't know the right way to handle it. -
Gabe over 13 yearsWhat do
T
andY
stand for? -
TheSpy over 13 years
T
andY
are Generic placeholders for specific types. In the OPs case they both would beInteger
-
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 over 13 years
String
would be the worst solution I could think of. The already mentionedPair
andint[]
are the apropriate solutions to the OPs problem. -
Gabe over 13 yearsJohannes Wachter: If it's just a best practice, then Stephen ought to have put "should" in bold instead of "has to".
-
Gabe over 13 years
T
stands for "type",V
stands for "value",E
stands for "element", but I have no idea whatY
stands for, and am unsure of thatT
stands for in the context ofY
. Are they just randomly chosen letters, or do they stand for some longer words? -
Jes over 13 yearsEdited to use Integer. You are both right, I missed that case.
-
Jaswinder Singh almost 3 yearsAs 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.