Answers for "different ways to create arrange words in alphabetical order on java program"

3

Java program to sort names in an alphabetical order

import java.util.Scanner;
public class SortNamesAlphabeticalOrder
{
   public static void main(String[] args)
   {
      int number;
      String str;
      Scanner sc1 = new Scanner(System.in);
      System.out.println("Please enter number of strings: ");
      number = sc1.nextInt();
      String[] names = new String[number];
      Scanner sc2 = new Scanner(System.in);
      System.out.println("Enter all strings: ");
      for(int a = 0; a < number; a++)
      {
         names[a] = sc2.nextLine();
      }
      for(int a = 0; a < number; a++)
      {
         for(int b = a + 1; b < number; b++)
         {
            // java alphabetical sort
            if(names[a].compareTo(names[b]) > 0)
            {
               str = names[a];
               names[a] = names[b];
               names[b] = str;
            }
         }
      }
      System.out.println("After sorting names in an alphabetical order: ");
      for(int a = 0; a < number - 1; a++)
      {
         System.out.println(names[a] + ", ");
      }
      System.out.print(names[number - 1]);
      sc1.close();
      sc2.close();
   }
}
Posted by: Guest on November-02-2020
2

Arrange words of a sentence in alphabetical order in java

// Arrange words of a sentence in alphabetical order in java
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class ArrangeInAlphabeticalOrder 
{
   public static void main(String[] args) 
   {
      Set set = new TreeSet();
      String strInput = "hi all welcome to flower brackets blog";
      System.out.println("Before arranging sentence in alphabetical order: " + strInput);
      StringTokenizer strToken = new StringTokenizer(strInput," ");
      while(strToken.hasMoreElements())
      {
         set.add(strToken.nextElement());
      }
      System.out.println("After arranging sentence in alphabetical order: " + set);
   }
}
Posted by: Guest on February-22-2021

Code answers related to "different ways to create arrange words in alphabetical order on java program"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language