Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Size
  3. {
  4. public static void main(String[] args)
  5. {
  6. int count;
  7. String temp;
  8. Scanner scan = new Scanner(System.in);
  9.  
  10. //User will be asked to enter the count of strings
  11. System.out.print("Enter number of strings you would like to enter:");
  12. count = scan.nextInt();
  13.  
  14.  
  15. String str[] = new String[count];
  16. Scanner scan2 = new Scanner(System.in);
  17.  
  18. //User is entering the strings and they are stored in an array
  19. System.out.println("Enter the Strings one by one:");
  20. for(int i = 0; i < count; i++)
  21. {
  22. str[i] = scan2.nextLine();
  23. }
  24. scan.close();
  25. scan2.close();
  26.  
  27. //Sorting the strings
  28. for (int i = 0; i < count; i++)
  29. {
  30. for (int j = i + 1; j < count; j++) {
  31. if (str[i].compareTo(str[j])>0)
  32. {
  33. temp = str[i];
  34. str[i] = str[j];
  35. str[j] = temp;
  36. }
  37. }
  38. }
  39.  
  40. //Displaying the strings after sorting them based on alphabetical order
  41. System.out.print("Strings in Sorted Order:");
  42. for (int i = 0; i <= count - 1; i++)
  43. {
  44. System.out.print(str[i] + ", ");
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement