0

I am attaching following code and i want to sort this as

 class sort{
    public static void main(String args[])  
    {String a ="this is a kiran";  
    StringTokenizer st =new StringTokenizer(a);  
    List f=new ArrayList();  
    f.add(st);  
    Collections.sort(f);  
    System.out.println("after sortting "+f);  
    }
 }

I want output as:

a
is 
kiran 
this

But i am getting an exception as:-
Exception in thread "main" java.lang.ClassCastException: java.util.StringTokenizer cannot be cast to java.lang.Comparableat java.util.Collections.sort(UnknownSource)atcom.sort.main(Sort.java:18)

4
  • 2
    why not use string.split("\\s+") instead of String tokenizer? Commented Jan 21, 2014 at 14:17
  • 1
    You can't just add the StringTokenizer to your list. Use the StringTokenizer to iterate through the tokens. Commented Jan 21, 2014 at 14:18
  • 2
    The StringTokenizer class does not implement the Comparable interface. "All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list)." Commented Jan 21, 2014 at 14:18
  • 1
    @ZouZou - excellent point... Commented Jan 21, 2014 at 14:20

4 Answers 4

4

Change your code. There are some mistakes you need to correct.

    String a ="this is a kiran";
    StringTokenizer st =new StringTokenizer(a);
    List<String> f=new ArrayList<>(); // use String type list
    while (st.hasMoreTokens()){ // add all tokens by iterating st
        f.add(st.nextToken()); // add tokens to list
    }
    Collections.sort(f);
    System.out.println("after sorting "+f);

Out put:

   after sorting [a, is, kiran, this]

Now you are getting sorted list

Sign up to request clarification or add additional context in comments.

Comments

2

The problem is here:

f.add(st);

You are adding StringTokenizer to the list, instead of adding the individual tokens. Changing the code to use generics would have helped: if you declare your List as List<String>, the code wouldn't compile, pointing you in the right direction:

List<String> f=new ArrayList<String>();

Add a while loop to collect tokens from st, and add them to f one by one.

P.S. Since this is almost certainly a learning exercise, I am not going to spoil the fun for you by completing your code.

Comments

1

You can also use hasMoreElements() and nextElement()

class sort{
   public static void main(String args[])  {
       String a ="this is a kiran";
        StringTokenizer st =new StringTokenizer(a);
        ArrayList<String> f=new ArrayList<String>(); // use String type list
        while (st.hasMoreElements()){ // add all by iterating st
        f.add((String) st.nextElement()); // add tokens to list
    }
    Collections.sort(f);
    System.out.println("after sorting "+f);
    }
 }

Comments

0

Consider using String.split() to split your string. Class names should be nouns, method names should be verbs or verb phrases, and you should use generics.

public static void main(String[] arguments)
{
    String input = "this is hootberry sause";  
    String[] inputArray;
    List<String> inputList = new ArrayList<String>();

    inputArray = input.split(" ");
    Collections.addAll(inputList, inputArray);
    Collections.sort(inputList);

    System.out.println("Before sort: " + input);
    System.out.println("After sort: " + inputList);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.