By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

How to append firstname to lastname?

P: 5
Hi..
Following is the code i worked on to sort the lastnames.I need to know how to append firstname along with middlename to respective lastname. What I need to do to get the specified output? Thanks.
Expand|Select|Wrap|Line Numbers
  1.  while ((s= in.readLine()) != null){
  2.            String[] names = s.split(" ");
  3.  
  4.            lastname[e] = names[names.length-1];
  5.  
  6.            System.out.println(lastname[e]);
  7.            e++;
  8.             }
  9.            for (int i=0;i<e-1;i++){
  10.  
  11.              for (int j=0;j<e-1;j++){
  12.  
  13.              if(lastname[i].compareTo(lastname[j])<0)
  14.                     {
  15.                         String temp= lastname[j];
  16.                         lastname[j]= lastname[i];
  17.                         lastname[i]=temp;
  18.                     }
  19.              }
  20.          }
  21.            for(int i=0; i<e-1; i++)
  22.            {
  23.                System.out.println(lastname[i]);
  24.            }
  25.  
  26.  
  27.  
  28.  
Mar 22 '13 #1
Share this Question
Share on Google+
5 Replies


10K+
P: 13,264
You add string together using +

So do firstName + middleName + lastName;
Mar 22 '13 #2

P: 5
If I say firstName + middleName + lastName; I get output as null.
Mar 22 '13 #3

10K+
P: 13,264
So explain where those variables are coming from and post the relevant code.
Mar 22 '13 #4

P: 5
I read the names from a file and split it at space after the middle name.I do not know how to fetch the firstname and middle name for respective lastname.I m stuck at this place.
Mar 22 '13 #5

10K+
P: 13,264
If the file contains the names separated by space e.g

Robert Louis Stevenson

Then when you do
Expand|Select|Wrap|Line Numbers
  1. String[] names = "Robert Louis Stevenson".split(" "); 
you get back an array where
names[0] contains "Robert" and names[1] contains "Louis"
and names[2] contains "Stevenson"
Mar 22 '13 #6

Post your reply

Sign in to post your reply or Sign up for a free account.