Hello I need to make a program that splits a phrase and afther that it sort it in alphabetical order. My teacher suggest that we shoud use StringTokenizer for the split and comapreTo for the sort.
So far I have splited it and now I am struggling with the sorting part.
public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("Afara este soare si frumos");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
It results: "1Afara 2este 3soare 4si 5frumos"
and afther the sorting part shoud be: "1Afara 2este 3frumos 4si 5soara"
Any advice woud be good! Thanks in advance!
Try this:
String mystring = "Afara este soare si frumos";
String[] sortedArray = mystring.split("\\s");
Arrays.sort(sortedArray);
System.out.println(Arrays.toString(sortedArray));