How to write the following code correctly?
public String toString(int[] position, int xOffset, int yOffset) {
String postn = String.format("[%d,%d]", position[0], position[1]);
String movm = String.format("[%d,%d]", xOffset, yOffset);
return (postn, movm);
}
Following error came up
movm cannot be returned.
No, you can not have two returns in a function, the first return will exit the function you will need to create an object.
Java doesn't support multi-value returns. We can use following solutions to return multiple values. We can return an array in Java. Below is a Java program to demonstrate the same.
Python functions can return multiple values. These values can be stored in variables directly. A function is not restricted to return a variable, it can return zero, one, two or more values.
When using Java 8 you could make use of the Pair class.
private static Pair<String, String> foo (/* some params */) {
final String val1 = ""; // some calculation
final String val2 = ""; // some other calculation
return new Pair<>(val1, val2);
}
Otherwise simply return an array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With