Type Here to Get Search Results !

Write a Java Program to sort an array of Strings.

 import java.util.*; 

 public class sortingstrings{ 

 public static void main(String[] argsv){ 

 Scanner s = new Scanner(System.in); 

 System.out.print("How many data items do you wish to enter: ");  int num = new Integer(s.nextLine()).intValue(); 

 String[] names = new String[num]; 

 System.out.println(); 

 int i = 0; 

 while(i < num){ 

 names[i] = s.nextLine(); 

 i++; 

 } 

 StringComparator strcomp = new StringComparator(); 

 Arrays.sort(names, strcomp);

System.out.println("Sorting List is:"); 

 for (String name: names){ 

System.out.println(name); 

  

 } 

 } 

 } 

 class StringComparator implements Comparator<String>{  public int compare(String a, String b){ 

 return a.compareTo(b); 

 } 

 } 

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Featured post

M

M