Count number of vowels in a string in Java

时间:2016-02-12 21:14:58

标签: java arrays if-statement

Im trying to create a method that counts the number of vowels in a string except there is a flaw in my code that I am unable to identify.

For example, if I put in the word fire, it prints out:

Vowels: uVowels: e

I'm not sure why its not printing out the number of vowels. Heres my code:

public static void main(String[] args) {

    for(int t = 0, t < NUMBER OF SECONDS YOU WANT; t++) {
        temperatureTest(YOUR INITIAL TEMP, t);
    }
}

}

3 个答案:

答案 0 :(得分:3)

It is printing "Vowels" multiple times because you have that inside the for loop, so every time the if statement is true, it is printing "Vowels" along with the vowel you want. You can print "Vowels" before beginning the for loop to fix that (note, this is best under an assumption whatever you are checking will have vowels in it, otherwise it will just print "Vowels:" with nothing trailing).

You also forgot to declare a variable to keep track of the number of vowels. Since you already are checking to print the vowels you see, you should add to that counter as you print each vowel you find looping through.

// components of the position vector for each vertex are stored
// contiguously in the buffer.
for ( var i = 0; i < vertexPositions.length; i++ )
{
    vertices[ i*3 + 0 ] = vertexPositions[i][0];
    vertices[ i*3 + 1 ] = vertexPositions[i][1];
    vertices[ i*3 + 2 ] = vertexPositions[i][2];
}

EDIT: Also note that the call to print the number of vowels is outside of the for loop, otherwise you will just print 1 2 3 4 ...." whenever you are in the loop, and I am assuming that is not intended behavior.

答案 1 :(得分:1)

You are not counting the vocals. Your variable v is a char. You should have an acumulator and increment it.

 $ dirs=(af am)
 $ for d in ${dirs[@]}
 > do
 > mkdir languages/$d
 > cp languages/copy-this/copy-this.txt languages/$d
 > done

Edit: you can use println so you dont have this problem "Vowels: uVowels: e "

it will print:

Vowels: u

Vowels: e

答案 2 :(得分:0)

所以我尝试保持代码的格式和一般结构相同(使用名为Counter的类)。这是一种做到这一点的方法相当简单(虽然保持类名称Counter使它看起来有点不稳定)

import java.util.Scanner;
public class Counter
{
  // class variables shared by more than one method
  public String word; // Word to be check for vowels
  private static final char[] vowels = {'a', 'e', 'i', 'o', 'u'}; // char[] of vowels to be checked against


  /*String prompt;
  static String strUserResponse;*/

  // main method
  public static void main (String[] args)
  {
    Counter word = new Counter(); // Create new instance of Counter class
    word.word = args[0]; // Set the word of the Counter variable
    System.out.println(word.vowels()); // Print nubmer of vowels in word
  }
  /* Returns the nubmer of vowels in the word */
  public int vowels()
  {
    String lowerWord = this.word.toLowerCase(); // Make word lowercase to be able to check for uppercase vowels, too
    int count = 0;
    if (word.length() > 0) {
      for (int i = 0; i < word.length(); i++) {
        count += (this.isVowel(lowerWord.charAt(i)) ? 1 : 0); // if the letter in the current iteration for the loop is a vowel, increase the count, otherwise, don't
      }
    }
    return count;
  }

  private static boolean isVowel(char c) {
    for (char ch : vowels) {
      // If the character is a vowel, return true
      if (c == ch) return true;
      else continue; // continue so if the char is not a vowel it doesn't just return false
    }
    return false; // if no vowels are found, return false
  }
 }