Java Program

Object:-Write a Program in java to sort string in alphabetical order.

import java.util.Scanner;
public class JavaProgram
{
 public static void main(String[] input)
 {
  int i, j;
  String temp;
  Scanner scan = new Scanner(System.in);        
  String names[] = new String[5];         
  System.out.print("Enter 5 Names/Words : ");
  for(i=0; i<5; i++)
  {
   names[i] = scan.nextLine();
  }              
  System.out.println("nSorting Words/Names in Alphabetical Order...n");
  for(i=0; i<5; i++)
  {
   for(j=1; j<5; j++)
   {
    if(names[j-1].compareTo(names[j])>0)
    {
     temp=names[j-1];
     names[j-1]=names[j];
     names[j]=temp;
    }
   }
  }              
  System.out.print("Words/Names Sorted in Alphabetical Order Successfully..!!");             
  System.out.println("nNow the List is :n");
  for(i=0;i<5;i++)
  {
   System.out.println(names[i]);
  }
 }
}

Output

Enter 5 Names/Words : Saurabh
Anoop
Harsh
Alok
Tanuj

Sorting Words/Names in Alphabetical Order...
Words/Names Sorted in Alphabetical Order Successfully..!!

Now the List is :
Alok
Anoop
Harsh
Saurabh
Tanuj
 

Explanation

In this  Program, it has been ask to the user to enter any five string like names to sort them in alphabetical order then display the sorted string in alphabetical order on the screen.








You may also like: