0

I have a function in Java, but I'm trying to return 3 elements from a function, but so far no luck.

public String retrieveStudentInfo() {
    String name = "Miley";
    String grade = "56"
    String ID = "1043"
    return name,grade,ID; //trying to return all the elements in this function.
}

based on the function above, I'm trying to return the name,grade and ID at the same time, through a variable like below:

String getStudentInfo = this.retrieveStudentInfo();

Could anyone please help me on this. I've specified the return data type as String but it wouldn't let me return them.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Maxxx
  • 3,688
  • 6
  • 28
  • 55

6 Answers6

3

You should wrap this properties in some class and then return instead of String the object of class like StudentInfo

public class StudentInfo {
    String name;
    String grade;
    String ID;
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Piotr S
  • 137
  • 3
2

It doesn't work because you can't return tuples in java. You can do like this:

  1. Declare a class, StudentInfo, which contains 3 attributes of type String - name, grade & ID.
  2. Define the return value of retrieveStudentInfo to be StudentInfo.
  3. Create an instance of it in the last line of the retrieveStudentInfo and return it.
Gal Naor
  • 2,397
  • 14
  • 17
1
public String[] retrieveStudentInfo() {
    String name = "Miley";
    String grade = "56"
    String ID = "1043"
    String[] data = new String[] {name ,grade ,ID};
    return data;
}
Alon Adler
  • 3,984
  • 4
  • 32
  • 44
  • 2
    This is a bad idea, because in the real world, `grade` would be an `int`, and then you can't use a `String` array. You'll either have to generalize to `Object[]` with boxing and casting, or use a better solution. – RealSkeptic Apr 28 '19 at 09:44
  • 1
    @RealSkeptic I understand and totally agree with what you say. But, my intention here is giving a basic answer to a basic question. – Alon Adler Apr 28 '19 at 09:46
1

If you want to return this object as String you can do something like this: return name + “ “ + grade + “ “ + ID;

It will be complicated however to interpret this String. So better is to return the complete Object.

Axel Podehl
  • 4,034
  • 29
  • 41
0

You could create an object called AnswerObject:

Define this object to have 3 public properties of String:

public class AnswerObject {
public String Name;
public String Grade;
public String ID;
}

Return this object from your method and you will have an answer with 3 elements.

Yishai Seela
  • 123
  • 5
0

User Tuple from vavr

private static Tuple3<Integer, String, Object> returnThreeElements() {
    return Tuple.of(1, "HelloWorld", new Object());
}

get element from Tuple can use method like the follow code:

public static void main(String[] args) {
    Tuple3<Integer, String, Object> result = returnThreeElements();
    System.out.println(result._1());
    System.out.println(result._2());
    System.out.println(result._3());
}
TongChen
  • 1,414
  • 1
  • 11
  • 21