Guest User

Untitled

a guest
May 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class SortAlphabetically {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. List<Character> alpha = new ArrayList<Character>();
  9.  
  10. System.out.println("Enter a word or phrase");
  11. StringBuilder input = new StringBuilder(scanner.next());
  12. scanner.close();
  13.  
  14. for (int i = 0; i < input.length(); i++) {
  15. if (Character.isWhitespace(input.charAt(i))) {
  16. input.deleteCharAt(i);
  17. } else {
  18. alpha.add(input.charAt(i));
  19. }
  20. }
  21.  
  22. alpha.sort(null);
  23. System.out.println("Input sorted alphabetically: " + alpha);
  24. }
  25. }
  26.  
  27. Enter a word or phrase
  28. cba fed
  29. Input sorted alphabetically: [a, b, c]
Add Comment
Please, Sign In to add comment