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]);
}
}
}
}