Tuesday, March 23, 2010

Arrange Names From Array Alphabetically

import java.io.*;

public class namearranger
{ public static void main(String args[]) throws IOException
{ InputStreamReader read = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(read);
String arr[] = new String[5];
System.out.println("Enter 5 names.");
for(int i=0;i<5;i++)
{ arr[i]=br.readLine();
}
String temp=null;
System.out.println("They will now be arranged alphabetically");
for(int i=0;i<5;i++)
{ for(int j=0;j<4;j++)
{ if((int)arr[j].charAt(0) > (int)arr[j+1].charAt(0))
{ temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int i=0;i<5;i++)
{ System.out.println(arr[i]);
}
}
}

No comments:

Post a Comment