单词排序

来源:互联网 发布:mac个人用户设置 编辑:程序博客网 时间:2024/04/30 09:26

/**

 *从文本中抽取单词并按字典序进行排序。

 * */

import java.lang.reflect.*;

import java.util.Arrays;

 

public class stringSort {

 

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

String text = "To be or not to be";

int spaces = 0,vowels = 0,letters = 0;

Character ch;

int i = 0, length;

length = text.length();

while(i < length)

{

ch = text.charAt(i++);

if(ch == 'A' || ch == 'E' || ch == 'I'|| ch == 'O' || ch == 'U' || ch == 'a' || ch == 'e' || ch == 'i'|| ch == 'o' || ch == 'u')

vowels++;

else if(ch == ' ')

spaces++;

else if(ch.isLetter(ch))

letters++;

}

//System.out.println("The text contained vowels: " + vowels + "/n"+ "consonants " + letters + "/n"+  "spaces " + spaces);

String str1 = "The text contained vowels: " + vowels + " consonants " + letters + " spaces " + spaces;

//String str1 = "i am a good boy";

String[] str = str1.split(" ");

Arrays.sort(str);

for(String s : str)

System.out.println(s);

 

}

 

}