简体   繁体   中英

Use a double “return” in a method

I'm creating a method where I have to fill two different arrays. One with people's names, one with people's surnames.

To do this I use this program: In this way, I create the two arrays in two separate methods and then I can use them in other methods.

public static void main(String[] args) {
    Scanner in=new Scanner(System.in);
    int person;
    System.out.println("How many people are there?");
    person=in.nextInt();
    String [] names=names(person);
    String [] surnames=surnames(person);
}

public static String [] names (int person) {
    Scanner in=new Scanner(System.in);
    String names []=new String [person];
    System.out.println("Enter the names of the people");
    for (int i=0; i<names.length; i++)
        names[i]=in.next();
    return names;
}
public static String [] surnames (int person) {
    Scanner in=new Scanner(System.in);
    String surnames []=new String [person];
    System.out.println("Enter the surnames of the people");
    for (int i=0; i<surnames.length; i++)
        surnames[i]=in.next();
    return surnames;
}

However, I would like to create the two arrays in the same method and then be able to use them in other methods.

There is certainly some faster and more intuitive method. Could anyone help me? Thanks.

You need to refactor the message to client (String):

public static String [] names (int person, String message) {
    Scanner in=new Scanner(System.in);
    String names []=new String [person];
    System.out.println(message);
    for (int i=0; i<names.length; i++)
        names[i]=in.next();
    return names;
}

And call the same method twice:

String [] names=names("Enter the names of the people");
String [] surnames=names("Enter the surnames of the people");

assuming you don't like to use a Person object (or something similar) - Consider using a wrapper object with two arrays in it.

Something like that:

class Wrapper{

     String [] names
     String [] surnames

    // getters & setters
}

And your combined method will return the wrapper object.

You can use the return statement with the if condition or switch case to return different value but you have to keep in mind that a function can only call return statement once.. once the return statement is called the function ends.

And the other alternaitve would be to send a parameter while calling the function so that you know what to ask.

public static String [] getNameOrSurname(int person, String value) {
    Scanner in=new Scanner(System.in);
    String retrunValue[]=new String [person];
    System.out.println("Enter the +"value"+ of the people");
    for (int i=0; i<returnValue.length; i++)
        returnValue[i]=in.next();
    return returnValue;
}

when you need to call the function to get the names

String [] names = getNameOrSurname(person , 'Name')

when you need to call the function to get the surname

String [] surnames = getNameOrSurname(person , 'Surname')

The best method is to use getters and setters:

Set function:

public static String [] setNames (int persons, String message) {
    Scanner in=new Scanner(System.in);
    String names []=new String [persons];
    System.out.println(message); 
    for (int i=0; i<names.length; i++)
        names[i]=in.next();
    return names;
}

Get function:

display the array

So each function does one thing, one gets values to the array and fills it. The second function shows it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM