Gnosis
Gnosis

Reputation: 61

Java Returning and array/returning multiple data types

I created my array in a method and some variables that need to be used by other methods. I'm not sure if you can return multiple values. If anyone can point me in the right direction so I can finish this you help would be greatly appreciated. The part I'm having problems with is enclosed in **

import java.util.Scanner;

public class Project1{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Project1 project1 = new Project1();
project1.mainMenu();


}//main


public void enterStudents(){
    Scanner input = new Scanner(System.in);
    System.out.println("Enter number of students");
    int numOfStudents = input.nextInt();
    Student[] students = new Student[numOfStudents];
    int i;
    for(i = 0; i <= numOfStudents - 1; i++){
        System.out.println("Enter student's ID: ");
        int id = input.nextInt();
        System.out.println("Enter student's first name: ");
        String first = input.next();
        System.out.println("Enter student's last name: ");
        String last = input.next();
        System.out.println("Enter student's class: ");
        String stuClass = input.next();
        Student x = new Student(id,first,last,stuClass);
        students[i] = x;
    }


}

**public void retrieveStuId(){
    Scanner input = new Scanner(System.in);
    System.out.println("Enter student id");
    int searchID = input.nextInt();
    int i;
    for(i = 0; i <= numOfStudents - 1; i++){
        int search = students[i].getId;
        if (studentID = searchID)
            System.out.println(students[i].toString());

    }**

Upvotes: 1

Views: 8141

Answers (8)

Kingz
Kingz

Reputation: 5286

The concept of data transfer objects (DTO) or Value Objects (VO) can be employed to return multiple data types from a method invocation. Construct your DTO/VO as POJOs with fields corresponding to the different data types you want to return; then populate the DTO instance in your method, and return that DTO.

Note that DTO is just a fancy term - any data structure capable of holding references to multiple data types simultaneously should work.

Upvotes: 0

Cris
Cris

Reputation: 5007

Declare

Student[] students ;

member variable for Project1 class

public class Project1 {


    Student[] students ;
...

Then

public void retrieveStuId(){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter student id");
        int searchID = input.nextInt();    
        for (int j = 0; j < students.length; j++) {
            Student student = students[j];
             int search = student.getId();
                if (search == searchID)
                    System.out.println(student.toString());
        }

    }

add this to the main menu

public void mainMenu() {
        Scanner input = new Scanner(System.in);
        System.out.println("1 - Enter student info");
        System.out.println("2 - Retrieve student by ID");
        System.out.println("3 - Retrieve student by last name");
        System.out.println("4 - Update student");
        System.out.println("5 - Exit");
        int menuSelect = input.nextInt();

        switch (menuSelect) {
        case 1:
            enterStudents();
            break;
        case 2:
            System.out.print("case 2");
            retrieveStuId();

            break;
        case 3:
            System.out.print("case 3");
            break;
        case 4:
            System.out.print("case 4");
            break;
        default:
            System.out.println("That is not a option");

        }
        mainMenu();
    }

To stay in the same execution ....

Upvotes: 2

Ishant Goyal
Ishant Goyal

Reputation: 1

You can use different data structures like Array, ArrayList, Vector as return type of method and put your multiple values in it.

Upvotes: 0

Roger Lindsj&#246;
Roger Lindsj&#246;

Reputation: 11543

You can only return one value, but that value can be a reference to an object which in turn has references to other objects. So it is possible to return an array, an list etc.

However, your method retrieveStuId looks as it is supposed to find one student (with matching id), so it could be logical for that method to return one student. Then the method needs to return the found student (or null or any other "not found" indicator. Something along the lines

public Student retrieveStuId() {
    int searchId = askForStudentId();
    for (Student student: students) {
        if (student.getId == searchId) {
          return student;
        }
    }
    // No student found
    return null;
}

Upvotes: 1

AusCBloke
AusCBloke

Reputation: 18492

You can't return multiple values in Java, however you have a couple of alternatives.

To "return" multiple values of the same type:

  • Return an array
  • Return a container, such as an ArrayList

For multiple values of different types:

  • Return an object, such as a Student object, which contains/wraps multiple instance variables such as firstName and lastName, which may be what you want to have modified and returned. Or for example a Point class that might wrap two coordinates.
  • Pass objects to a method and modify those objects, since all objects are passed by reference in Java. If you need to pass a primitive, such as an int or double, you can use the wrapper classes Integer and Double.

However, you haven't really told us what you're trying/wanting to do, making that code snippet essentially pointless.

Upvotes: 1

bezmax
bezmax

Reputation: 26132

In Java a method can return an Object, primitive or an array. If you need to return multiple values you usually use an array or some List.

For example (arrays):

int[] getManyValues() {
    int resultCount = getResultCount();
    int[] result = new int[resultCount];

    for (int i=0;i<resultCount;i++) {
        result[i] = getNextResult();
    }

    return result;
}

Or with List:

List<Integer> getManyValues() {
    List<Integer> result = new ArrayList<Integer>();

    while (hasMoreResults()) {
        result.add(getNextResult());
    }

    return result;
}

Upvotes: 2

Ashwinee K Jha
Ashwinee K Jha

Reputation: 9317

You can return only one value. But that one value can be an array/list etc. So you can return students from the method and use it without any problem.

Upvotes: 1

nfechner
nfechner

Reputation: 17525

It is not possible to return multiple values. But you can easily create a wrapper class, that can take all of the values, and return that.

Upvotes: 0

Related Questions