3

i want to print the first and last name of a person. The question is how can I return both first and last name. Below is my code.

package methodbasics;
import java.util.Scanner;

public class MethodBasics {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.print("Enter Your FName :");
        String firstName = input.nextLine();
        System.out.print("Enter Your LName :");
        String lastName = input.nextLine();
        System.out.printf("Welcome %s %s!!!\n", printName(firstName,           lastName));
    }

    public static String printName(String firstName, String lastName){          
        return firstName, lastName; 
    }
}
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
Sirmee
  • 79
  • 4
  • 3
    Use a container like array or List and then add these values to it and return the container – TheLostMind Feb 07 '16 at 16:02
  • 1
    return new String[] {firstName, lastName}; – Johannes Jander Feb 07 '16 at 16:11
  • 4
    Create a `Person` class with these attributes and return an instance of the class from the method. – Mick Mnemonic Feb 07 '16 at 16:15
  • Some very good responses can be found here: http://stackoverflow.com/questions/15285270/is-it-possible-to-return-more-than-one-value-from-a-method-in-java?lq=1 – The Roy Feb 07 '16 at 17:27
  • these answers don't work with OP's `printf( %s %s )`.... – ZhongYu Feb 07 '16 at 17:30
  • @TheLostMind That is **terrible** advice. He should be using a class, which will have named/typed properties, not emulating a tuple with a list, which only has meaningless indices. – BlueRaja - Danny Pflughoeft Feb 07 '16 at 17:58
  • @BlueRaja-DannyPflughoeft - That depends on the use case. Class is also a container BTW – TheLostMind Feb 07 '16 at 17:59
  • The answer is: you can't. Java is an OO language. So the real workaround (which is not even a workaround because not being able to return 2 values from a method is normal and expected) is in the name: _object_. But note that in your code, you don't even need a method to begin with. Just do `System.out.printf("Welcome %s %s!!!\n", firstName, lastName));`. Or, make a method `printName(String firstName, String lastName)` but this method will do the `System.out.printf`: separation of concern. – Tunaki Feb 07 '16 at 19:35

3 Answers3

8
  1. Use a Container class, for example

    public class FirstAndLastName {
        String firstName;
        String lastName;
        ... // constructor and getter/setter
    }
    public static FirstAndLastName printName(String firstName, String lastName) {
        return new FirstAndLastName(firstName, lastName); 
    }
    public static void main(String[] args) {
        ...
        FirstAndLastName firstAndLastName = printName(firstName, lastName);
        ... firstAndLastName.getFirstName();
    
  2. Use List or an array, for example

    public static List<String> printName(String firstName, String lastName) {          
        return Arrays.asList(firstName, lastName); 
    }
    private static final int FIRST_NAME = 0;...
    public static void main(String[] args) {
        ...
        List list = printName(firstName, lastName);
        ... list.get(0)...list.get(FIRST_NAME);
    

    or

    public static String[] printName(String firstName, String lastName) {          
        return new String[] {firstName, lastName}; 
    }
    private static final int FIRST_NAME = 0;...
    public static void main(String[] args) {
        ...
        String[] array = printName(firstName, lastName);
        ... array[0]...array[FIRST_NAME];
    
  3. Use Map, for example

    public static Map<String, String> printName(String firstName, String lastName) {  
        Map<String, String> result = new HashMap<>(2);
        result.put("firstName", firstName);           
        result.put("lastName", lastName);
        return result; 
    }
    public static void main(String[] args) {
        ...
        Map map = printName(firstName, lastName);
        ... map.get("firstName")...map.get("lastName");
    
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
6

You can try creating something like pair object, and return it.

public class Pair<A, B> {
  private A first;
  private B second;

  public Pair(A first, B second) {
    this.first = first;
    this.second = second;
  }

  ......
  ......
}
Amrendra Kumar
  • 326
  • 2
  • 5
-1

Just concatenate the variables into one string:

return firstName + " " + lastName;
Nate
  • 1,330
  • 1
  • 13
  • 23
  • 1
    And what about a name whit a space in his or her name? – H. Pauwelyn Feb 07 '16 at 18:30
  • It would still work just fine. If the last name was "Van Dyke", for instance, and the first name was "Jack", then the above code would return "Jack Van Dyke". The other solutions are great, especially if the coder still needs to be able to access first and last names separately. But if he/she just wants to print the combined names, then it's just as simple as what I suggest above. – Nate Feb 07 '16 at 18:34
  • Why downvotes, btw? This solution is simple, but does what the user wants... – Nate Feb 07 '16 at 18:36
  • and what if the first name is Eddy Wally and the lastname Van de walle. Your code will return Eddy Wally Van de walle. How can you split this name again for get the firstname only? – H. Pauwelyn Feb 07 '16 at 18:39
  • I've downvoted because my previous comment. – H. Pauwelyn Feb 07 '16 at 18:40
  • To me, that sounds different from what the OP was asking. Maybe that's just me – Nate Feb 07 '16 at 18:40
  • Thanks for the explanation, though – Nate Feb 07 '16 at 18:40
  • Thanks Nate, your answer has solved my problem. But is concatenation the only way to return two values? I am thinking may be there are some situation whereby concatenation may not work. Thank you. @Nate – Sirmee Feb 07 '16 at 21:07
  • Viacheslav's answer is the most complete. If you still want the two values to work as two individual values, then one of his solutions is better. My solution simply lets you return two strings as one, which is what I thought you needed in my first read through of your question. New programmers might be looking for simple fixes like that, but you're right -- my solution is definitely limited. – Nate Feb 07 '16 at 21:14
  • OK @Nate, thank you very much for showing interest in my question. – Sirmee Feb 07 '16 at 21:19