1
Public class Example1
{

    String fullname(String firstName, String secondName)
    {
        return firstName,secondName;
        //return fullname(firstName,secondName); doesn't work either :(
    }

    public static void main(String[] args) 
    {
        Example1 myname=new Example();
        System.out.println(myname.fullname("John", "Wick");
    }
}

I got an error though, when I run my program. Can you help me out please guys? thanks a lot

4
  • return an array of strings Commented Apr 28, 2015 at 9:05
  • What do you expect to be printed in System.out.println(myname.fullname("John", "Wick"));? Commented Apr 28, 2015 at 9:09
  • If you want to print as a result "John Wick" then you should probably change your question from "how to return two strings" into something more like "how to return one string containing two strings". Otherwise people will suggest to return instead of string one object which will store both string inside (like array, list, set, or instance of any other class which would have at least two String fields). Commented Apr 28, 2015 at 9:21
  • @Pshemo yeah you are right.I kinda missed the title. I'm a new user in Java though Commented Apr 28, 2015 at 9:34

4 Answers 4

5

A method could return only one value at a time. Either concatenate the both the strings and return or you could use a class. set the details and return the instance of that class

Approach 1:

 return firstname+"|"+secondname;

If you want those separate in calling method just use return_value.split("|"). Now you get a array of strings with 2 elements, firstname and secondname

Approach 2:

Create a class like

 public class Person{
   private String firstName;
   private String secondName;
   public Person(String fname, String sname){
      firstname = fname; 
      secondname = sname;
    }
   // getter setters
 }

in the method do

 Person p = new Person(firstname, secondname);
 return p;
Sign up to request clarification or add additional context in comments.

2 Comments

I normally use Approach 2
Thanks for helping me. I've already noticed your 2.suggest dude .Very kind of you.
5

You cannot return 2 strings at a time. You can use String array though like this:

 public String[] fullname(String firstName, String secondName){

      String[] names = new String[2];
      names[0] = firstName;
      names[1] = secondName;
      return names;
 }

Another logical way to solve this issue is to concatenate both of them and split them.

You can use also use list

 List<String> nameList =  new ArrayList<String>();
 nameList.add(firstName);

and get the elements like nameList.get(0) and so on

You can use

 Map<String,String> nameMap = new HashMap<String,String>();
 nameMap.put("firstName",firstName);
 nameMap.put("secondName",secondName);

There are a number of ways to use data structures to solve this issue.

Comments

2

A method has a single return type, so unless you package the two Strings in some class and return that class (or put the two Strings in a String array or some other container of Strings), you can only return one String.

If your only requirement is to display the full name as a single String, you can simply concatenate the two Strings :

String fullname (String firstName, String secondName) 
{
    return firstName + " " + secondName;
}

4 Comments

Thanks a lot I've read a lot of books etc but I've never seen that code 'return firstName + " " + secondName;'
@Vikjan This is still one String which gets returned. If you want to have separate Strings you have to change the return type.
@Vikjan "I've read a lot of books etc" ware there Java books? It is hard to believe that in any Java book there wouldn't be information about string concatenation.
Probably you should understand the concept clearly along with reading
2

Well you have three options

  1. concatenate the strings and return as a single string. Split the string at the receiving end.

  2. Return String array

  3. Return an object . ie set the two srings to two member variables and return the object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.