2012-12-05 49 views
7

我有这个问题的作业(我是诚实的,不是试图隐藏它,至少) 而我有问题搞清楚如何做到这一点。使用字符串方法来查找和计算字符串中的元音?

鉴于以下声明:String phrase =“WazzUp? - Who's On FIRST ??? - IDUNNO”;将必要的代码写入 ,计算字符串中元音的数量并将适当的消息输出到屏幕上。

这里是我到目前为止的代码:

String phrase = " WazzUp ? - Who's On FIRST ??? - IDUNNO"; 
int i, length, vowels = 0; 
String j; 
length = phrase.length(); 
for (i = 0; i < length; i++) 
{ 

    j = phrase.substring(i, i++); 
    System.out.println(j); 

    if (j.equalsIgnoreCase("a") == true) 
    vowels++; 
    else if (j.equalsIgnoreCase("e") == true) 
    vowels++; 
    else if (j.equalsIgnoreCase("i") == true) 
    vowels++; 
    else if (j.equalsIgnoreCase("o") == true) 
    vowels++; 
    else if (j.equalsIgnoreCase("u") == true) 
    vowels++; 

} 
System.out.println("Number of vowels: " + vowels); 

然而,当我运行它,它只是使一堆空行。谁能帮忙?

+3

'String.char在这种情况下,()'会比'String.substring()'容易。 – NullUserException

+1

看起来像一个switch语句可以在这里实现。 –

+0

而不是开关,最好是创建一个元音列表,然后检查是否list.contains(j) – Gaskoin

回答

8

phrase.substring(i, i++);应该是phrase.substring(i, i + 1);

i++给出i的值,然后加1。正如您现在所知,String j实际上是phrase.substring(i, i);,它始终是空字符串。

您不需要更改for循环体中的i的值,因为它已经在for (i = 0; i < length; i++)中增加了。

+0

几乎我要发布。除了我不认为最后一个增量是必要的。 – Wes

+0

@Wes你说得对。谢谢。 – irrelephant

+0

哇,非常感谢! – xSpartanCx

0

i ++使用后增加i,所以你基本上是说string.substring(i,i)。由于结束标记是独占的,这将始终返回一个空字符串。一个简单的办法是只将其更改为

j = phrase.substring(i, ++i); 

希望帮助!

+0

我不认为需要有一个增量操作符前缀或后缀。但是,它有用于了解前缀和后缀运算符的作用。 – Wes

8

我看不到在循环中需要打印语句。

String s = "Whatever you want it to be.".toLowerCase(); 
int vowelCount = 0; 
for (int i = 0, i < s.length(); ++i) { 
    switch(s.charAt(i)) { 
     case 'a': 
     case 'e': 
     case 'i': 
     case 'o': 
     case 'u': 
      vowelCount++; 
      break; 
     default: 
      // do nothing 
    } 
} 

这将字符串转换为小写,并检查字符串中所有字符的元音。

+5

为什么在每个循环迭代中创建新的情人案例字符串?在循环之前创建一个会更有效。 – Pshemo

+0

好点。更新。 – Whymarrh

+0

'.toLowercase()'不对,应该是'。toLowerCase()' – MortalMan

-1

公共类JavaApplication2 {

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) throws IOException { 
    // TODO code application logic here 
    System.out.println("Enter some text"); 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    String input = br.readLine().toLowerCase(); 

    char[] vowel = new char[]{'a', 'e', 'i', 'o', 'u'}; 
    int[] countVowel = new int[5]; 
    for (int j = 0; j < input.length(); j++) { 
     char c =input.charAt(j); 
     if(c=='a') 
      countVowel[0]++; 
     else if(c=='e') 
      countVowel[1]++; 
     else if(c=='i') 
      countVowel[2]++; 
     else if(c=='o') 
      countVowel[3]++; 
     else if(c=='u') 
      countVowel[4]++; 


    } 
    for (int i = 0; i <countVowel.length; i++) { 
      System.out.println("Count of vowel " + vowel[i] + "=" + countVowel[i]); 
     } 

    } 
} 
7

这可能/应该是一个班轮

System.out.println("the count of all vowels: " + (phrase.length() - phrase.replaceAll("a|e|i|o|u", "").length())); 

及其字符串仅方法

2

改进上的上述回答考虑帽元音:

System.out.println(s.length() - s.toLowerCase().replaceAll("a|e|i|o|u|", "").length());