I'm new to java.I write the code and dont know why its wrong.Someone can point out and modify it for me.thanks alot.
Here the code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package OOP;

import java.util.Arrays;
import java.util.Scanner;

/**
 *
 * @author lamborghini
 */
public class SortName {

    /**
     * @param args the command line arguments
     */
    
    public static void main(String[] args) {
       Scanner in= new Scanner(System.in);
       System.out.println("How many people do you want to input?");//Enter the number
       int Length=Integer.parseInt(in.nextLine());
       String[] FirstName= new String[Length];
       String[] LastName= new String [Length];
       String[] MiddleName=new String[Length];
       String[] Name= new String[Length];

    
       for(int j=0;j<Length;j++){
           int Spaces=0;
           int Words=0;
          System.out.println("Enter you Name: ");
          Name[j]=in.nextLine();         
          System.out.println("You have: "+Name[j]);
            StringBuffer name= new StringBuffer(Name[j]);
             name.setCharAt(0,Character.toUpperCase(name.charAt(0)));
         if(Character.isWhitespace(name.charAt(j))){
                Spaces++;
                name.setCharAt(j+1,Character.toUpperCase(name.charAt(j+1)));//capitalize the letter after space
            }
         if(Spaces==0) {
             LastName[j]=name.substring(0,name.length());
             FirstName[j]=" ";
             MiddleName[j]=" ";
         }
         else if(Spaces==1){
             name.setCharAt(name.indexOf(" ")+1, Character.toUpperCase(name.charAt(name.indexOf(" ")+1)));
             LastName[j]=name.substring(0,name.indexOf(" "));
             FirstName[j]=name.substring(name.indexOf(" ")+1,name.length());
         }
         else if(Spaces>1){
        for(int i=0;i<name.length();i++){

            if(name.charAt(i)==' '){
                name.setCharAt(i+1, Character.toUpperCase(name.charAt(i+1)));
                Words++;
            }

        }
         
            MiddleName[j]= name.substring(Name[j].indexOf(' ')+1,Name[j].lastIndexOf(' '));
            LastName[j]=name.substring(0, Name[j].indexOf(' '));
            FirstName[j]=name.substring(Name[j].lastIndexOf(' '),name.length());
         }
        System.out.println("Capitalize name: "+name);
        System.out.print("Middle name is: "+MiddleName[j]+"\n");
        System.out.print("First name: "+FirstName[j]+"\n");
        System.out.print("Last name: "+LastName[j]+"\n");
        System.out.println("Number of Words :"+ (Words+1));
        
      }

       String[] FirstName1=new String[Length];
       System.arraycopy(FirstName,0, FirstName1,0, Length);
       Arrays.sort(FirstName1);
       String[] LastName1= new String[Length];
       System.arraycopy(LastName,0, LastName1,0, Length);
       Arrays.sort(LastName1);

       for(int m=0;m<Length;m++){
           if(FirstName1[0]==FirstName[m]){
               System.out.println("Smallest: "+LastName[m]+MiddleName[m]+FirstName[m]);
           
           }
       }
       
    }

}

Hi

If you use a SringTokenizer whit whitespace as tokenizer the input string will be cut after each whitespace and you can more easily capitalize the first letter in every name if that is what you want.

If you want to capitalize every letter in the name you have to catch every char in the string and let it be capitalized.

It feels as if you do not really know what you catch in all your variables. Try to work whit simple System.out.println(""+variable) after every step to see what you actually are holding in your variables. This way you will find what is not working and from there you can redesign your program.

Your question was a bit vague but I hope this will give you some help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.