user2984143
user2984143

Reputation: 85

How to send multiple values using methods in Java

I am trying to send multiple values using arrays, I am using return to send two different data types, but I know it is the wrong way of doing it. I have research but I can't find the solution for this specific problem. Any help would be appreciated. Thanks.

import java.util.Scanner;

public class methodbankinput {
    public static void main(String args[]) {

        Scanner kb = new Scanner(System.in); 
        String[] names = new String[2];
        int[] account_numbers = new int[2];
        String userinput = "";
        String accountosearch="";
        int count = 0;

        do {
                System.out.println("Type P");
                System.out.println("Type S");
                System.out.println("Type E to exit");
                userinput = kb.nextLine();

                if(userinput.equals("P")) {
                   **String[] populate** = populate(names,account_numbers);

                } else if(userinput.equals("S")) {

                System.out.println("Please enter the accoun to search");
                accountosearch = kb.next(); 
                search(names,accounttosearch);
                } 
                count++;
           }
           while(count >=0);
} 

public static String[] int[] populate(String[] names,int[] account_numbers) {   
    Scanner kb = new Scanner(System.in); 
        for (int i = 0; i < names.length; i++) {
            System.out.println("Please enter 10 names");
            names[i] = kb.next();
            System.out.println("Please Enter 10 Account_numbers");
            account_numbers[i] = kb.nextInt();
        }
        return names[10],account_numbers[10];    
    }
 }    

Upvotes: 0

Views: 458

Answers (2)

Felix Feisst
Felix Feisst

Reputation: 175

Java does not support methods to have more than one return type.

You can however return an Object[] array with two components which themselves are arrays again: a String[] array and an int[] array: return new Object[] { names, account_numbers };

Another way is to provide a special "tuple" class for your method which contains the two result values of interest. Such a tuple class can be compared to a struct in C:

public class NamesAccountNumbers {
  private String[] names;
  private int[] accountNumbers;

  /** constructor */
  public NamesAccountNumbers(String[] names, int[] accountNumbers) {
    this.names = names;
    this.accountNumbers = accountNumbers;
  }

  public String[] getNames() {
    return names;
  }

  public int[] getAccountNumbers() {
    return accountNumbers;
  }

}

It is possible to make the two fields of this class public and providing no constructor and getters, which would result in a struct like object. However, it is considered good practice to obey the Java-Beans-Style.

Upvotes: 1

SLaks
SLaks

Reputation: 888233

You can't do that.

Instead, you should create an Account class with properties for the name and account number, then return Account[].

Upvotes: 3

Related Questions