Main Method:
package districts;
import java.util.Scanner;
/**
*
* @author Sabit
*/
public class Districts
{
public static void main(String[] args)
{
Scanner S = new Scanner(System.in);
int size;
Sort srt;
System.out.println("Enter Total Case: ");
size =S.nextInt();
String dist[] = new String[size];
System.out.println("Enter District Name: ");
S.nextLine();
for(int i=0;i<dist.length;i++ )
{
dist[i] = S.nextLine();
}
srt = new Sort(dist);
srt.sortDistricts();
srt.display();
srt.count1stIndex();
}
}
Sort Class:
package districts;
/**
*
* @author Sabit
*/
public class Sort
{
public String str[];
char array[]=new char[26];
int count[] = new int[26];
public void count1stIndex()
{
for(int i=0;i<26;i++)
{
array[i] = (char) ('A'+i);
}
for(int i = 0;i<26;i++)
{
count[i]=0;
}
for(int i=0;i<str.length;i++)
{
String temp = str[i];
for(int j=0;j<26;j++)
{
if(temp.charAt( 0 )==array[j] || temp.charAt(0)== array[j]+32)
{
count[j]++;
}
}
}
System.out.println("Totla First Letter: ");
for(int i=0;i<26;i++)
{
if(count[i]>0)
{
System.out.println(array[i]+" = "+count[i]);
}
}
}
public Sort(String s[])
{
this.str = s;
}
public void sortDistricts()
{
for(int i =0;i<str.length;i++)
{
for(int j=i+1;j<str.length;j++)
{
if((str[i].compareTo(str[j]))>0)
{
String temp = str[i];
str[i]= str[j];
str[j]=temp;
}
}
}
}
public void display()
{
System.out.println("Your Sorted Element: ");
for(String s : str)
{
System.out.println(s);
}
}
}
Output
Enter total case:
5
Enter district name;
Nrayanganj
Narshindhi
Dhaka
Sylhet
Chittagonj
Your sorted element:
Chittagonj
Dhaka
Nrayanganj
Narshindhi
Sylhet
Total first letter:
C=1
D=1
N=2
S=1
Add your comment