How to append firstname to lastname?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • praver235
    New Member
    • Feb 2013
    • 5

    How to append firstname to lastname?

    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.
    Code:
     while ((s= in.readLine()) != null){
               String[] names = s.split(" ");
    
               lastname[e] = names[names.length-1];
    
               System.out.println(lastname[e]);
               e++;
                }
               for (int i=0;i<e-1;i++){
    
                 for (int j=0;j<e-1;j++){
    
                 if(lastname[i].compareTo(lastname[j])<0)
                        {
                            String temp= lastname[j];
                            lastname[j]= lastname[i];
                            lastname[i]=temp;
                        }
                 }
             }
               for(int i=0; i<e-1; i++)
               {
                   System.out.println(lastname[i]);
               }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    You add string together using +

    So do firstName + middleName + lastName;

    Comment

    • praver235
      New Member
      • Feb 2013
      • 5

      #3
      If I say firstName + middleName + lastName; I get output as null.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        So explain where those variables are coming from and post the relevant code.

        Comment

        • praver235
          New Member
          • Feb 2013
          • 5

          #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.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            If the file contains the names separated by space e.g

            Robert Louis Stevenson

            Then when you do
            Code:
            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"

            Comment

            Working...