Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class SortAlphabetically {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Character> alpha = new ArrayList<Character>();
- System.out.println("Enter a word or phrase");
- StringBuilder input = new StringBuilder(scanner.next());
- scanner.close();
- for (int i = 0; i < input.length(); i++) {
- if (Character.isWhitespace(input.charAt(i))) {
- input.deleteCharAt(i);
- } else {
- alpha.add(input.charAt(i));
- }
- }
- alpha.sort(null);
- System.out.println("Input sorted alphabetically: " + alpha);
- }
- }
- Enter a word or phrase
- cba fed
- Input sorted alphabetically: [a, b, c]
Add Comment
Please, Sign In to add comment