java数组元音字母和计数

时间:2015-02-02 22:23:43

标签: java string

这是我正在处理的一项工作的下面的代码。现在我必须修改我的程序,因此它会询问需要排序的名称,一旦程序完成,它需要按字母顺序显示名称,计算每个名称有多少个字符以及它们有多少个元音?

任何人都可以帮我一把吗?

String[] StudentNames = new String[8];
StudentNames[0] = "Joel";
StudentNames[1] = "Amy";
StudentNames[2] = "Robert";
StudentNames[3] = "Conor";
StudentNames[4] = "Mark";
StudentNames[5] = "Stewart";
StudentNames[6] = "Jack";
StudentNames[7] = "James";
for (int x = 0; x < 8; x++) {
    System.out.println(StudentNames[x]);
}

1 个答案:

答案 0 :(得分:0)

您需要实现Comparator:

public class YourClass implements Comparator<String>{

     public int compare(Object o1, Object o2) {

        String student1Name = ((String) o1); 
        String student2Name = ((String) o1); 

        int student1Vowels = countVowels(student1Name);
        int student2Vowels = countVowels(student2Name);

        if (student1Name == student2Name)
             return 0;
        if (student1Name > student2Name)
             return -1;
        if (student1Name < student2Name)
             return 1; 
    }   

    private int countVowels(String name){
       //return the vowels count
    }

}

这是一个适合您需要的例子,继续您的要求。