12

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.
ifloop
  • 8,079
  • 2
  • 26
  • 35
Lbert Hartanto
  • 131
  • 2
  • 2
  • 6
  • Hi. Can return Map.Entry, array of 2 strings, collection of 2 items, pass containers as parameters, etc. – nndru Oct 27 '14 at 10:42
  • return as an array. So your method signature will be `public String[].....`. Also, @Reimeus answered it..... – ha9u63a7 Oct 27 '14 at 10:42
  • See this : http://stackoverflow.com/questions/2832472/how-to-return-2-values-from-a-java-function – Rajarshi Goswami Oct 27 '14 at 10:44
  • In Java you can return only one variable/Object. If you want to return two String objects.I recommend you to put those in an array,list or set and then pass return that object. – User27854 Oct 27 '14 at 10:44

5 Answers5

33

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.

ifloop
  • 8,079
  • 2
  • 26
  • 35
  • pair class is part or `javafx`. Can we use in SE? – Subhrajyoti Majumder Oct 27 '14 at 10:49
  • @SubhrajyotiMajumder [As of JDK 7u6 JavaFX is included with the standard JDK and JRE bundles.](http://www.oracle.com/technetwork/java/javase/overview/javafx-overview-2158620.html) – ifloop Oct 27 '14 at 10:53
  • @DavidM. I agree with you. Especially when looking forward to Java9 modularization, where one might encounter runtimes without UI modules installed. An array or a simple pojo class could be more elegant. – ifloop Jul 26 '17 at 10:05
4

You cannot return two different values in methods in Java.

When this need occurs, you usually have two options:

  1. Create a class which has the types you want to return, and then return an instance of that class. Usually these classes are very basic, contain public members and optionally a constructor to initiate an instance with all the required values.
  2. Add a reference type object as a parameter to the method which the method will mutate with the values you want to return. In your example you use strings, and so this will not work since in Java strings are immutable.

Another option would be to use an array of strings, since both your return types are strings. You could use this option as a return value, or a parameter which is altered by the method.

Edit: usually "toString" methods do return only one string. Perhaps you should consider concatenating both your strings into one string using some separator. Example:

return postn + "_" + movm;
Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58
4

There are four method which I can think of to implement this scenario:

1. Using a class

Create a class with having all the attributes which you want to return, in your case it can be

public class Sample {
   private String postn;
   private String movm;
   public String getPostn() {
        return postn;
   }
   public void setPostn(String postn) {
    this.postn = postn;
   }
   public String getMovm() {
       return movm;
   }
   public void setMovm(String movm) {
       this.movm = movm;
   }    
}

and change your method to include this class's object and set the values in these attributes. Change the return type of the method to Class name e.g. Sample

public Sample 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);
    Sample obj = new Sample();
    obj.setPostn(postn);
    obj.setMovm(movm);
    return obj;
}

2. Using an array

Change the method to return String array and store the values in that array:

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);
    String arr[] = new String[2];
    arr[0] = postn;
    arr[1] = movm;
    return arr;
}

3. Using javatuples

You can read details at link http://www.javatuples.org/

4. By delimiting Data

You can use some delimiter if you are sure about the data in the variables. But this will require one spiting with the delimiter, when you want to use these values.

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;
}
Ashu
  • 2,066
  • 3
  • 19
  • 33
2

In Java, when you want a function to return multiple values, you must

  • embed those values in an object you return
  • or change an object that is passed to your function ~ dystroy

You have two options (that I know of) here:

Option 1: Return as a new array:

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

        string [] myArray = new string[2];
        myArray[0] = postn;
        myArray[1] = movm;

        return myarray; //returns as array
     }

Option 2: Return as such

Edited to show get set

    private String a;
    private String b;

    public void setPostN(String s)
    {
            a= s;
    }

    public String getPostN()
    {
            return a;
    }

    public void setMovm(String s)
    {
            a= s;
    }

    public String getMovm() 
    {
            return a;
    }

with your method:

public void 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);

        setPostN(postn);
        setMovm(movm);

       //no return, but available via calling getMovm() or getPostN()
     }
Community
  • 1
  • 1
1

you can either create a wrapper type, instantiate and return it, or return a Map or List/Array.

Kent
  • 189,393
  • 32
  • 233
  • 301