如何在字符串中获取单个字母

时间:2015-04-15 16:24:53

标签: java

我对如何搜索字符串数组中的单个字母(特定元音)感到困惑。

import static java.lang.System.*;

import java.util.Arrays;

public class Word {
    private String word;
    private static String vowels = "AEIOUaeiou";   //only one

    public Word() {
        String vow = "";
        vowels = vow;
    }

    public Word(String wrd) {
        word=wrd;
    }

    public void setWord(String wrd) {
    }

    public int getNumVowels(String[] ray, String vowels) {
        int count=0;
        for(String item : ray) {
            if() {
                count = count + 1;
            }
        }
        return count;
    }

    public int getLength(String[] ray) {
        return 0;
    }

    public String toString(String[] ray) {
        return "";
    }
}

这是跑步者。如何通过输入搜索单个字母来获取NumVowels?

import java.util.Arrays;
import java.util.Scanner;
import static java.lang.System.*;

public class WordRunner {
    public static void main(String[] args) {
        String alpha="";
        String [] ray = new String[4];
        ray[0] ="Polo";
        ray[1] ="Crazy";
        ray[2] ="Abracadabra";
        ray[3] ="Awesome";

       out.println(Arrays.toString(ray));
       out.println("Number of vowels: " + Arrays.getNumVowels(ray));  
       out.println("Length of the word: " + Arrays.getLength(ray));
    }
}

3 个答案:

答案 0 :(得分:1)

您的代码中有多处错误......

您正在调用不存在的Arrays方法......

out.println("Number of vowels: "+ Arrays.getNumVowels(ray));  
out.println("Length of the word: "+ Arrays.getLength(ray));

修改Word课程(你提出了很多不必要的东西):

import static java.lang.System.*;

import java.util.Arrays;

public class Word
{
    private static String vowels = "AEIOUaeiou";   //only one


    public static int getNumVowels(String word)
    {
        int count=0;
        for (int i=0; i < word.length(); i++)
        {
            // check if selected char is a vowel
            if (vowels.contains(word.charAt(i) + ""))
            {
                count ++; // same as count = count + 1
            }
        }
        return count;
    }

}

然后调用方法,因为它是静态的:

public static void main(String[] args)
{
     System.out.println("Number of vowels of Polo: "+ Word.getNumVowels("Polo"));   
     //Output:
     //         Number of vowels of Polo: 2

     // or execute like this:
     String a = "Polo";
     int vowels = Word.getNumVowels(a);
     System.out.printlm("Number of vowels of Polo: "+ vowels);
     //Output:
     //         Number of vowels of Polo: 2
}

更新:如果您希望单词类计算元数中的元音,只需使用已有的方法。

Word内部类中添加接收数组并使用旧数组的现有方法的重载:

// overload: same method name, but different arguments!!!
public static int getNumVowels(String[] array)
{
    int count = 0;
    for (i = 0; i < array.lenght; i ++){
        // use existing method to save code!! 
        count += getNumVowels(array[i]);
    }

    return count;
}

并使用它:

String [] ray = new String[4];
ray[0] ="Polo";
ray[1] ="Crazy";
ray[2] ="Abracadabra";
ray[3] ="Awesome";


int vowels = Word.getNumVowels(ray);  // count vowels in ALL array words...

答案 1 :(得分:0)

你可以从字符串中获取char数组并在其上循环寻找所需的元音(如果你想计算单个元音出现的次数,那么就不需要传递一个字符串,你可以只需使用char)。

public int getNumVowels(String[] ray, char vowel) {
    int count=0;
    for(String s: ray)
    {
        char[] array = s.toLowerCase().toCharArray();
        for (int i=0; i<array.length; i++) {
            if(array[i] == vowel) {
                count = count + 1;
            }
        }


    }
    return count;
}

仅当您希望搜索不区分大小写时才需要s.toLowerCase()(因为例如&#39; a&#39;不同于&#39; A&#39;)。

答案 2 :(得分:-1)

直截了当的方式:

String vowels = "AEIOUaeiou";
    int count = 0;
    for (int i=0; i<str.length(); i++)
    {
        if (vowels.contains(str.charAt(i) + ""))
        {
            count++;
        }
    }
    return count;